Merge 3.18-rc3 into staging-next
[cascardo/linux.git] / drivers / staging / lustre / lustre / llite / llite_lib.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/llite/llite_lib.c
37  *
38  * Lustre Light Super operations
39  */
40
41 #define DEBUG_SUBSYSTEM S_LLITE
42
43 #include <linux/module.h>
44 #include <linux/statfs.h>
45 #include <linux/types.h>
46 #include <linux/mm.h>
47
48 #include "../include/lustre_lite.h"
49 #include "../include/lustre_ha.h"
50 #include "../include/lustre_dlm.h"
51 #include "../include/lprocfs_status.h"
52 #include "../include/lustre_disk.h"
53 #include "../include/lustre_param.h"
54 #include "../include/lustre_log.h"
55 #include "../include/cl_object.h"
56 #include "../include/obd_cksum.h"
57 #include "llite_internal.h"
58
59 struct kmem_cache *ll_file_data_slab;
60 struct proc_dir_entry *proc_lustre_fs_root;
61
62 static LIST_HEAD(ll_super_blocks);
63 static DEFINE_SPINLOCK(ll_sb_lock);
64
65 #ifndef log2
66 #define log2(n) ffz(~(n))
67 #endif
68
69 static struct ll_sb_info *ll_init_sbi(void)
70 {
71         struct ll_sb_info *sbi = NULL;
72         unsigned long pages;
73         unsigned long lru_page_max;
74         struct sysinfo si;
75         class_uuid_t uuid;
76         int i;
77
78         sbi = kzalloc(sizeof(*sbi), GFP_NOFS);
79         if (!sbi)
80                 return NULL;
81
82         spin_lock_init(&sbi->ll_lock);
83         mutex_init(&sbi->ll_lco.lco_lock);
84         spin_lock_init(&sbi->ll_pp_extent_lock);
85         spin_lock_init(&sbi->ll_process_lock);
86         sbi->ll_rw_stats_on = 0;
87
88         si_meminfo(&si);
89         pages = si.totalram - si.totalhigh;
90         if (pages >> (20 - PAGE_CACHE_SHIFT) < 512) {
91                 lru_page_max = pages / 2;
92         } else {
93                 lru_page_max = (pages / 4) * 3;
94         }
95
96         /* initialize lru data */
97         atomic_set(&sbi->ll_cache.ccc_users, 0);
98         sbi->ll_cache.ccc_lru_max = lru_page_max;
99         atomic_set(&sbi->ll_cache.ccc_lru_left, lru_page_max);
100         spin_lock_init(&sbi->ll_cache.ccc_lru_lock);
101         INIT_LIST_HEAD(&sbi->ll_cache.ccc_lru);
102
103         sbi->ll_ra_info.ra_max_pages_per_file = min(pages / 32,
104                                            SBI_DEFAULT_READAHEAD_MAX);
105         sbi->ll_ra_info.ra_max_pages = sbi->ll_ra_info.ra_max_pages_per_file;
106         sbi->ll_ra_info.ra_max_read_ahead_whole_pages =
107                                            SBI_DEFAULT_READAHEAD_WHOLE_MAX;
108         INIT_LIST_HEAD(&sbi->ll_conn_chain);
109         INIT_LIST_HEAD(&sbi->ll_orphan_dentry_list);
110
111         ll_generate_random_uuid(uuid);
112         class_uuid_unparse(uuid, &sbi->ll_sb_uuid);
113         CDEBUG(D_CONFIG, "generated uuid: %s\n", sbi->ll_sb_uuid.uuid);
114
115         spin_lock(&ll_sb_lock);
116         list_add_tail(&sbi->ll_list, &ll_super_blocks);
117         spin_unlock(&ll_sb_lock);
118
119         sbi->ll_flags |= LL_SBI_VERBOSE;
120         sbi->ll_flags |= LL_SBI_CHECKSUM;
121
122         sbi->ll_flags |= LL_SBI_LRU_RESIZE;
123
124         for (i = 0; i <= LL_PROCESS_HIST_MAX; i++) {
125                 spin_lock_init(&sbi->ll_rw_extents_info.pp_extents[i].
126                                pp_r_hist.oh_lock);
127                 spin_lock_init(&sbi->ll_rw_extents_info.pp_extents[i].
128                                pp_w_hist.oh_lock);
129         }
130
131         /* metadata statahead is enabled by default */
132         sbi->ll_sa_max = LL_SA_RPC_DEF;
133         atomic_set(&sbi->ll_sa_total, 0);
134         atomic_set(&sbi->ll_sa_wrong, 0);
135         atomic_set(&sbi->ll_agl_total, 0);
136         sbi->ll_flags |= LL_SBI_AGL_ENABLED;
137
138         return sbi;
139 }
140
141 static void ll_free_sbi(struct super_block *sb)
142 {
143         struct ll_sb_info *sbi = ll_s2sbi(sb);
144
145         if (sbi != NULL) {
146                 spin_lock(&ll_sb_lock);
147                 list_del(&sbi->ll_list);
148                 spin_unlock(&ll_sb_lock);
149                 OBD_FREE(sbi, sizeof(*sbi));
150         }
151 }
152
153 static int client_common_fill_super(struct super_block *sb, char *md, char *dt,
154                                     struct vfsmount *mnt)
155 {
156         struct inode *root = NULL;
157         struct ll_sb_info *sbi = ll_s2sbi(sb);
158         struct obd_device *obd;
159         struct obd_capa *oc = NULL;
160         struct obd_statfs *osfs = NULL;
161         struct ptlrpc_request *request = NULL;
162         struct obd_connect_data *data = NULL;
163         struct obd_uuid *uuid;
164         struct md_op_data *op_data;
165         struct lustre_md lmd;
166         u64 valid;
167         int size, err, checksum;
168
169         obd = class_name2obd(md);
170         if (!obd) {
171                 CERROR("MD %s: not setup or attached\n", md);
172                 return -EINVAL;
173         }
174
175         data = kzalloc(sizeof(*data), GFP_NOFS);
176         if (!data)
177                 return -ENOMEM;
178
179         osfs = kzalloc(sizeof(*osfs), GFP_NOFS);
180         if (!osfs) {
181                 OBD_FREE_PTR(data);
182                 return -ENOMEM;
183         }
184
185         if (proc_lustre_fs_root) {
186                 err = lprocfs_register_mountpoint(proc_lustre_fs_root, sb,
187                                                   dt, md);
188                 if (err < 0)
189                         CERROR("could not register mount in /proc/fs/lustre\n");
190         }
191
192         /* indicate the features supported by this client */
193         data->ocd_connect_flags = OBD_CONNECT_IBITS    | OBD_CONNECT_NODEVOH  |
194                                   OBD_CONNECT_ATTRFID  |
195                                   OBD_CONNECT_VERSION  | OBD_CONNECT_BRW_SIZE |
196                                   OBD_CONNECT_MDS_CAPA | OBD_CONNECT_OSS_CAPA |
197                                   OBD_CONNECT_CANCELSET | OBD_CONNECT_FID     |
198                                   OBD_CONNECT_AT       | OBD_CONNECT_LOV_V3   |
199                                   OBD_CONNECT_RMT_CLIENT | OBD_CONNECT_VBR    |
200                                   OBD_CONNECT_FULL20   | OBD_CONNECT_64BITHASH|
201                                   OBD_CONNECT_EINPROGRESS |
202                                   OBD_CONNECT_JOBSTATS | OBD_CONNECT_LVB_TYPE |
203                                   OBD_CONNECT_LAYOUTLOCK |
204                                   OBD_CONNECT_PINGLESS |
205                                   OBD_CONNECT_MAX_EASIZE |
206                                   OBD_CONNECT_FLOCK_DEAD |
207                                   OBD_CONNECT_DISP_STRIPE;
208
209         if (sbi->ll_flags & LL_SBI_SOM_PREVIEW)
210                 data->ocd_connect_flags |= OBD_CONNECT_SOM;
211
212         if (sbi->ll_flags & LL_SBI_LRU_RESIZE)
213                 data->ocd_connect_flags |= OBD_CONNECT_LRU_RESIZE;
214 #ifdef CONFIG_FS_POSIX_ACL
215         data->ocd_connect_flags |= OBD_CONNECT_ACL | OBD_CONNECT_UMASK;
216 #endif
217
218         if (OBD_FAIL_CHECK(OBD_FAIL_MDC_LIGHTWEIGHT))
219                 /* flag mdc connection as lightweight, only used for test
220                  * purpose, use with care */
221                 data->ocd_connect_flags |= OBD_CONNECT_LIGHTWEIGHT;
222
223         data->ocd_ibits_known = MDS_INODELOCK_FULL;
224         data->ocd_version = LUSTRE_VERSION_CODE;
225
226         if (sb->s_flags & MS_RDONLY)
227                 data->ocd_connect_flags |= OBD_CONNECT_RDONLY;
228         if (sbi->ll_flags & LL_SBI_USER_XATTR)
229                 data->ocd_connect_flags |= OBD_CONNECT_XATTR;
230
231 #ifdef HAVE_MS_FLOCK_LOCK
232         /* force vfs to use lustre handler for flock() calls - bug 10743 */
233         sb->s_flags |= MS_FLOCK_LOCK;
234 #endif
235 #ifdef MS_HAS_NEW_AOPS
236         sb->s_flags |= MS_HAS_NEW_AOPS;
237 #endif
238
239         if (sbi->ll_flags & LL_SBI_FLOCK)
240                 sbi->ll_fop = &ll_file_operations_flock;
241         else if (sbi->ll_flags & LL_SBI_LOCALFLOCK)
242                 sbi->ll_fop = &ll_file_operations;
243         else
244                 sbi->ll_fop = &ll_file_operations_noflock;
245
246         /* real client */
247         data->ocd_connect_flags |= OBD_CONNECT_REAL;
248         if (sbi->ll_flags & LL_SBI_RMT_CLIENT)
249                 data->ocd_connect_flags |= OBD_CONNECT_RMT_CLIENT_FORCE;
250
251         data->ocd_brw_size = MD_MAX_BRW_SIZE;
252
253         err = obd_connect(NULL, &sbi->ll_md_exp, obd, &sbi->ll_sb_uuid,
254                           data, NULL);
255         if (err == -EBUSY) {
256                 LCONSOLE_ERROR_MSG(0x14f, "An MDT (md %s) is performing "
257                                    "recovery, of which this client is not a "
258                                    "part. Please wait for recovery to complete,"
259                                    " abort, or time out.\n", md);
260                 goto out;
261         } else if (err) {
262                 CERROR("cannot connect to %s: rc = %d\n", md, err);
263                 goto out;
264         }
265
266         sbi->ll_md_exp->exp_connect_data = *data;
267
268         err = obd_fid_init(sbi->ll_md_exp->exp_obd, sbi->ll_md_exp,
269                            LUSTRE_SEQ_METADATA);
270         if (err) {
271                 CERROR("%s: Can't init metadata layer FID infrastructure, "
272                        "rc = %d\n", sbi->ll_md_exp->exp_obd->obd_name, err);
273                 goto out_md;
274         }
275
276         /* For mount, we only need fs info from MDT0, and also in DNE, it
277          * can make sure the client can be mounted as long as MDT0 is
278          * available */
279         err = obd_statfs(NULL, sbi->ll_md_exp, osfs,
280                         cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
281                         OBD_STATFS_FOR_MDT0);
282         if (err)
283                 goto out_md_fid;
284
285         /* This needs to be after statfs to ensure connect has finished.
286          * Note that "data" does NOT contain the valid connect reply.
287          * If connecting to a 1.8 server there will be no LMV device, so
288          * we can access the MDC export directly and exp_connect_flags will
289          * be non-zero, but if accessing an upgraded 2.1 server it will
290          * have the correct flags filled in.
291          * XXX: fill in the LMV exp_connect_flags from MDC(s). */
292         valid = exp_connect_flags(sbi->ll_md_exp) & CLIENT_CONNECT_MDT_REQD;
293         if (exp_connect_flags(sbi->ll_md_exp) != 0 &&
294             valid != CLIENT_CONNECT_MDT_REQD) {
295                 char *buf;
296
297                 buf = kzalloc(PAGE_CACHE_SIZE, GFP_KERNEL);
298                 obd_connect_flags2str(buf, PAGE_CACHE_SIZE,
299                                       valid ^ CLIENT_CONNECT_MDT_REQD, ",");
300                 LCONSOLE_ERROR_MSG(0x170, "Server %s does not support "
301                                    "feature(s) needed for correct operation "
302                                    "of this client (%s). Please upgrade "
303                                    "server or downgrade client.\n",
304                                    sbi->ll_md_exp->exp_obd->obd_name, buf);
305                 OBD_FREE(buf, PAGE_CACHE_SIZE);
306                 err = -EPROTO;
307                 goto out_md_fid;
308         }
309
310         size = sizeof(*data);
311         err = obd_get_info(NULL, sbi->ll_md_exp, sizeof(KEY_CONN_DATA),
312                            KEY_CONN_DATA,  &size, data, NULL);
313         if (err) {
314                 CERROR("%s: Get connect data failed: rc = %d\n",
315                        sbi->ll_md_exp->exp_obd->obd_name, err);
316                 goto out_md_fid;
317         }
318
319         LASSERT(osfs->os_bsize);
320         sb->s_blocksize = osfs->os_bsize;
321         sb->s_blocksize_bits = log2(osfs->os_bsize);
322         sb->s_magic = LL_SUPER_MAGIC;
323         sb->s_maxbytes = MAX_LFS_FILESIZE;
324         sbi->ll_namelen = osfs->os_namelen;
325         sbi->ll_max_rw_chunk = LL_DEFAULT_MAX_RW_CHUNK;
326
327         if ((sbi->ll_flags & LL_SBI_USER_XATTR) &&
328             !(data->ocd_connect_flags & OBD_CONNECT_XATTR)) {
329                 LCONSOLE_INFO("Disabling user_xattr feature because "
330                               "it is not supported on the server\n");
331                 sbi->ll_flags &= ~LL_SBI_USER_XATTR;
332         }
333
334         if (data->ocd_connect_flags & OBD_CONNECT_ACL) {
335 #ifdef MS_POSIXACL
336                 sb->s_flags |= MS_POSIXACL;
337 #endif
338                 sbi->ll_flags |= LL_SBI_ACL;
339         } else {
340                 LCONSOLE_INFO("client wants to enable acl, but mdt not!\n");
341 #ifdef MS_POSIXACL
342                 sb->s_flags &= ~MS_POSIXACL;
343 #endif
344                 sbi->ll_flags &= ~LL_SBI_ACL;
345         }
346
347         if (data->ocd_connect_flags & OBD_CONNECT_RMT_CLIENT) {
348                 if (!(sbi->ll_flags & LL_SBI_RMT_CLIENT)) {
349                         sbi->ll_flags |= LL_SBI_RMT_CLIENT;
350                         LCONSOLE_INFO("client is set as remote by default.\n");
351                 }
352         } else {
353                 if (sbi->ll_flags & LL_SBI_RMT_CLIENT) {
354                         sbi->ll_flags &= ~LL_SBI_RMT_CLIENT;
355                         LCONSOLE_INFO("client claims to be remote, but server "
356                                       "rejected, forced to be local.\n");
357                 }
358         }
359
360         if (data->ocd_connect_flags & OBD_CONNECT_MDS_CAPA) {
361                 LCONSOLE_INFO("client enabled MDS capability!\n");
362                 sbi->ll_flags |= LL_SBI_MDS_CAPA;
363         }
364
365         if (data->ocd_connect_flags & OBD_CONNECT_OSS_CAPA) {
366                 LCONSOLE_INFO("client enabled OSS capability!\n");
367                 sbi->ll_flags |= LL_SBI_OSS_CAPA;
368         }
369
370         if (data->ocd_connect_flags & OBD_CONNECT_64BITHASH)
371                 sbi->ll_flags |= LL_SBI_64BIT_HASH;
372
373         if (data->ocd_connect_flags & OBD_CONNECT_BRW_SIZE)
374                 sbi->ll_md_brw_size = data->ocd_brw_size;
375         else
376                 sbi->ll_md_brw_size = PAGE_CACHE_SIZE;
377
378         if (data->ocd_connect_flags & OBD_CONNECT_LAYOUTLOCK) {
379                 LCONSOLE_INFO("Layout lock feature supported.\n");
380                 sbi->ll_flags |= LL_SBI_LAYOUT_LOCK;
381         }
382
383         if (data->ocd_ibits_known & MDS_INODELOCK_XATTR) {
384                 if (!(data->ocd_connect_flags & OBD_CONNECT_MAX_EASIZE)) {
385                         LCONSOLE_INFO(
386                                 "%s: disabling xattr cache due to unknown maximum xattr size.\n",
387                                 dt);
388                 } else {
389                         sbi->ll_flags |= LL_SBI_XATTR_CACHE;
390                         sbi->ll_xattr_cache_enabled = 1;
391                 }
392         }
393
394         obd = class_name2obd(dt);
395         if (!obd) {
396                 CERROR("DT %s: not setup or attached\n", dt);
397                 err = -ENODEV;
398                 goto out_md_fid;
399         }
400
401         data->ocd_connect_flags = OBD_CONNECT_GRANT     | OBD_CONNECT_VERSION  |
402                                   OBD_CONNECT_REQPORTAL | OBD_CONNECT_BRW_SIZE |
403                                   OBD_CONNECT_CANCELSET | OBD_CONNECT_FID      |
404                                   OBD_CONNECT_SRVLOCK   | OBD_CONNECT_TRUNCLOCK|
405                                   OBD_CONNECT_AT | OBD_CONNECT_RMT_CLIENT |
406                                   OBD_CONNECT_OSS_CAPA | OBD_CONNECT_VBR|
407                                   OBD_CONNECT_FULL20 | OBD_CONNECT_64BITHASH |
408                                   OBD_CONNECT_MAXBYTES |
409                                   OBD_CONNECT_EINPROGRESS |
410                                   OBD_CONNECT_JOBSTATS | OBD_CONNECT_LVB_TYPE |
411                                   OBD_CONNECT_LAYOUTLOCK | OBD_CONNECT_PINGLESS;
412
413         if (sbi->ll_flags & LL_SBI_SOM_PREVIEW)
414                 data->ocd_connect_flags |= OBD_CONNECT_SOM;
415
416         if (!OBD_FAIL_CHECK(OBD_FAIL_OSC_CONNECT_CKSUM)) {
417                 /* OBD_CONNECT_CKSUM should always be set, even if checksums are
418                  * disabled by default, because it can still be enabled on the
419                  * fly via /proc. As a consequence, we still need to come to an
420                  * agreement on the supported algorithms at connect time */
421                 data->ocd_connect_flags |= OBD_CONNECT_CKSUM;
422
423                 if (OBD_FAIL_CHECK(OBD_FAIL_OSC_CKSUM_ADLER_ONLY))
424                         data->ocd_cksum_types = OBD_CKSUM_ADLER;
425                 else
426                         data->ocd_cksum_types = cksum_types_supported_client();
427         }
428
429         data->ocd_connect_flags |= OBD_CONNECT_LRU_RESIZE;
430         if (sbi->ll_flags & LL_SBI_RMT_CLIENT)
431                 data->ocd_connect_flags |= OBD_CONNECT_RMT_CLIENT_FORCE;
432
433         CDEBUG(D_RPCTRACE, "ocd_connect_flags: %#llx ocd_version: %d "
434                "ocd_grant: %d\n", data->ocd_connect_flags,
435                data->ocd_version, data->ocd_grant);
436
437         obd->obd_upcall.onu_owner = &sbi->ll_lco;
438         obd->obd_upcall.onu_upcall = cl_ocd_update;
439
440         data->ocd_brw_size = DT_MAX_BRW_SIZE;
441
442         err = obd_connect(NULL, &sbi->ll_dt_exp, obd, &sbi->ll_sb_uuid, data,
443                           NULL);
444         if (err == -EBUSY) {
445                 LCONSOLE_ERROR_MSG(0x150, "An OST (dt %s) is performing "
446                                    "recovery, of which this client is not a "
447                                    "part.  Please wait for recovery to "
448                                    "complete, abort, or time out.\n", dt);
449                 goto out_md;
450         } else if (err) {
451                 CERROR("%s: Cannot connect to %s: rc = %d\n",
452                        sbi->ll_dt_exp->exp_obd->obd_name, dt, err);
453                 goto out_md;
454         }
455
456         sbi->ll_dt_exp->exp_connect_data = *data;
457
458         err = obd_fid_init(sbi->ll_dt_exp->exp_obd, sbi->ll_dt_exp,
459                            LUSTRE_SEQ_METADATA);
460         if (err) {
461                 CERROR("%s: Can't init data layer FID infrastructure, "
462                        "rc = %d\n", sbi->ll_dt_exp->exp_obd->obd_name, err);
463                 goto out_dt;
464         }
465
466         mutex_lock(&sbi->ll_lco.lco_lock);
467         sbi->ll_lco.lco_flags = data->ocd_connect_flags;
468         sbi->ll_lco.lco_md_exp = sbi->ll_md_exp;
469         sbi->ll_lco.lco_dt_exp = sbi->ll_dt_exp;
470         mutex_unlock(&sbi->ll_lco.lco_lock);
471
472         fid_zero(&sbi->ll_root_fid);
473         err = md_getstatus(sbi->ll_md_exp, &sbi->ll_root_fid, &oc);
474         if (err) {
475                 CERROR("cannot mds_connect: rc = %d\n", err);
476                 goto out_lock_cn_cb;
477         }
478         if (!fid_is_sane(&sbi->ll_root_fid)) {
479                 CERROR("%s: Invalid root fid "DFID" during mount\n",
480                        sbi->ll_md_exp->exp_obd->obd_name,
481                        PFID(&sbi->ll_root_fid));
482                 err = -EINVAL;
483                 goto out_lock_cn_cb;
484         }
485         CDEBUG(D_SUPER, "rootfid "DFID"\n", PFID(&sbi->ll_root_fid));
486
487         sb->s_op = &lustre_super_operations;
488 #if THREAD_SIZE >= 8192 /*b=17630*/
489         sb->s_export_op = &lustre_export_operations;
490 #endif
491
492         /* make root inode
493          * XXX: move this to after cbd setup? */
494         valid = OBD_MD_FLGETATTR | OBD_MD_FLBLOCKS | OBD_MD_FLMDSCAPA;
495         if (sbi->ll_flags & LL_SBI_RMT_CLIENT)
496                 valid |= OBD_MD_FLRMTPERM;
497         else if (sbi->ll_flags & LL_SBI_ACL)
498                 valid |= OBD_MD_FLACL;
499
500         op_data = kzalloc(sizeof(*op_data), GFP_NOFS);
501         if (!op_data) {
502                 err = -ENOMEM;
503                 goto out_lock_cn_cb;
504         }
505
506         op_data->op_fid1 = sbi->ll_root_fid;
507         op_data->op_mode = 0;
508         op_data->op_capa1 = oc;
509         op_data->op_valid = valid;
510
511         err = md_getattr(sbi->ll_md_exp, op_data, &request);
512         if (oc)
513                 capa_put(oc);
514         OBD_FREE_PTR(op_data);
515         if (err) {
516                 CERROR("%s: md_getattr failed for root: rc = %d\n",
517                        sbi->ll_md_exp->exp_obd->obd_name, err);
518                 goto out_lock_cn_cb;
519         }
520
521         err = md_get_lustre_md(sbi->ll_md_exp, request, sbi->ll_dt_exp,
522                                sbi->ll_md_exp, &lmd);
523         if (err) {
524                 CERROR("failed to understand root inode md: rc = %d\n", err);
525                 ptlrpc_req_finished(request);
526                 goto out_lock_cn_cb;
527         }
528
529         LASSERT(fid_is_sane(&sbi->ll_root_fid));
530         root = ll_iget(sb, cl_fid_build_ino(&sbi->ll_root_fid,
531                                             sbi->ll_flags & LL_SBI_32BIT_API),
532                        &lmd);
533         md_free_lustre_md(sbi->ll_md_exp, &lmd);
534         ptlrpc_req_finished(request);
535
536         if (root == NULL || IS_ERR(root)) {
537                 if (lmd.lsm)
538                         obd_free_memmd(sbi->ll_dt_exp, &lmd.lsm);
539 #ifdef CONFIG_FS_POSIX_ACL
540                 if (lmd.posix_acl) {
541                         posix_acl_release(lmd.posix_acl);
542                         lmd.posix_acl = NULL;
543                 }
544 #endif
545                 err = IS_ERR(root) ? PTR_ERR(root) : -EBADF;
546                 root = NULL;
547                 CERROR("lustre_lite: bad iget4 for root\n");
548                 goto out_root;
549         }
550
551         err = ll_close_thread_start(&sbi->ll_lcq);
552         if (err) {
553                 CERROR("cannot start close thread: rc %d\n", err);
554                 goto out_root;
555         }
556
557 #ifdef CONFIG_FS_POSIX_ACL
558         if (sbi->ll_flags & LL_SBI_RMT_CLIENT) {
559                 rct_init(&sbi->ll_rct);
560                 et_init(&sbi->ll_et);
561         }
562 #endif
563
564         checksum = sbi->ll_flags & LL_SBI_CHECKSUM;
565         err = obd_set_info_async(NULL, sbi->ll_dt_exp, sizeof(KEY_CHECKSUM),
566                                  KEY_CHECKSUM, sizeof(checksum), &checksum,
567                                  NULL);
568         cl_sb_init(sb);
569
570         err = obd_set_info_async(NULL, sbi->ll_dt_exp, sizeof(KEY_CACHE_SET),
571                                  KEY_CACHE_SET, sizeof(sbi->ll_cache),
572                                  &sbi->ll_cache, NULL);
573
574         sb->s_root = d_make_root(root);
575         if (sb->s_root == NULL) {
576                 CERROR("%s: can't make root dentry\n",
577                         ll_get_fsname(sb, NULL, 0));
578                 err = -ENOMEM;
579                 goto out_lock_cn_cb;
580         }
581
582         sbi->ll_sdev_orig = sb->s_dev;
583
584         /* We set sb->s_dev equal on all lustre clients in order to support
585          * NFS export clustering.  NFSD requires that the FSID be the same
586          * on all clients. */
587         /* s_dev is also used in lt_compare() to compare two fs, but that is
588          * only a node-local comparison. */
589         uuid = obd_get_uuid(sbi->ll_md_exp);
590         if (uuid != NULL) {
591                 sb->s_dev = get_uuid2int(uuid->uuid, strlen(uuid->uuid));
592                 get_uuid2fsid(uuid->uuid, strlen(uuid->uuid), &sbi->ll_fsid);
593         }
594
595         if (data != NULL)
596                 OBD_FREE_PTR(data);
597         if (osfs != NULL)
598                 OBD_FREE_PTR(osfs);
599
600         return err;
601 out_root:
602         iput(root);
603 out_lock_cn_cb:
604         obd_fid_fini(sbi->ll_dt_exp->exp_obd);
605 out_dt:
606         obd_disconnect(sbi->ll_dt_exp);
607         sbi->ll_dt_exp = NULL;
608         /* Make sure all OScs are gone, since cl_cache is accessing sbi. */
609         obd_zombie_barrier();
610 out_md_fid:
611         obd_fid_fini(sbi->ll_md_exp->exp_obd);
612 out_md:
613         obd_disconnect(sbi->ll_md_exp);
614         sbi->ll_md_exp = NULL;
615 out:
616         if (data != NULL)
617                 OBD_FREE_PTR(data);
618         if (osfs != NULL)
619                 OBD_FREE_PTR(osfs);
620         lprocfs_unregister_mountpoint(sbi);
621         return err;
622 }
623
624 int ll_get_max_mdsize(struct ll_sb_info *sbi, int *lmmsize)
625 {
626         int size, rc;
627
628         *lmmsize = obd_size_diskmd(sbi->ll_dt_exp, NULL);
629         size = sizeof(int);
630         rc = obd_get_info(NULL, sbi->ll_md_exp, sizeof(KEY_MAX_EASIZE),
631                           KEY_MAX_EASIZE, &size, lmmsize, NULL);
632         if (rc)
633                 CERROR("Get max mdsize error rc %d \n", rc);
634
635         return rc;
636 }
637
638 int ll_get_default_mdsize(struct ll_sb_info *sbi, int *lmmsize)
639 {
640         int size, rc;
641
642         size = sizeof(int);
643         rc = obd_get_info(NULL, sbi->ll_md_exp, sizeof(KEY_DEFAULT_EASIZE),
644                          KEY_DEFAULT_EASIZE, &size, lmmsize, NULL);
645         if (rc)
646                 CERROR("Get default mdsize error rc %d\n", rc);
647
648         return rc;
649 }
650
651 int ll_get_max_cookiesize(struct ll_sb_info *sbi, int *lmmsize)
652 {
653         int size, rc;
654
655         size = sizeof(int);
656         rc = obd_get_info(NULL, sbi->ll_md_exp, sizeof(KEY_MAX_COOKIESIZE),
657                           KEY_MAX_COOKIESIZE, &size, lmmsize, NULL);
658         if (rc)
659                 CERROR("Get max cookiesize error rc %d\n", rc);
660
661         return rc;
662 }
663
664 int ll_get_default_cookiesize(struct ll_sb_info *sbi, int *lmmsize)
665 {
666         int size, rc;
667
668         size = sizeof(int);
669         rc = obd_get_info(NULL, sbi->ll_md_exp, sizeof(KEY_DEFAULT_COOKIESIZE),
670                           KEY_DEFAULT_COOKIESIZE, &size, lmmsize, NULL);
671         if (rc)
672                 CERROR("Get default cookiesize error rc %d\n", rc);
673
674         return rc;
675 }
676
677 static void ll_dump_inode(struct inode *inode)
678 {
679         struct ll_d_hlist_node *tmp;
680         int dentry_count = 0;
681
682         LASSERT(inode != NULL);
683
684         ll_d_hlist_for_each(tmp, &inode->i_dentry)
685                 dentry_count++;
686
687         CERROR("inode %p dump: dev=%s ino=%lu mode=%o count=%u, %d dentries\n",
688                inode, ll_i2mdexp(inode)->exp_obd->obd_name, inode->i_ino,
689                inode->i_mode, atomic_read(&inode->i_count), dentry_count);
690 }
691
692 void lustre_dump_dentry(struct dentry *dentry, int recur)
693 {
694         struct list_head *tmp;
695         int subdirs = 0;
696
697         LASSERT(dentry != NULL);
698
699         list_for_each(tmp, &dentry->d_subdirs)
700                 subdirs++;
701
702         CERROR("dentry %p dump: name=%.*s parent=%.*s (%p), inode=%p, count=%u,"
703                " flags=0x%x, fsdata=%p, %d subdirs\n", dentry,
704                dentry->d_name.len, dentry->d_name.name,
705                dentry->d_parent->d_name.len, dentry->d_parent->d_name.name,
706                dentry->d_parent, dentry->d_inode, d_count(dentry),
707                dentry->d_flags, dentry->d_fsdata, subdirs);
708         if (dentry->d_inode != NULL)
709                 ll_dump_inode(dentry->d_inode);
710
711         if (recur == 0)
712                 return;
713
714         list_for_each(tmp, &dentry->d_subdirs) {
715                 struct dentry *d = list_entry(tmp, struct dentry, d_u.d_child);
716
717                 lustre_dump_dentry(d, recur - 1);
718         }
719 }
720
721 static void client_common_put_super(struct super_block *sb)
722 {
723         struct ll_sb_info *sbi = ll_s2sbi(sb);
724
725 #ifdef CONFIG_FS_POSIX_ACL
726         if (sbi->ll_flags & LL_SBI_RMT_CLIENT) {
727                 et_fini(&sbi->ll_et);
728                 rct_fini(&sbi->ll_rct);
729         }
730 #endif
731
732         ll_close_thread_shutdown(sbi->ll_lcq);
733
734         cl_sb_fini(sb);
735
736         list_del(&sbi->ll_conn_chain);
737
738         obd_fid_fini(sbi->ll_dt_exp->exp_obd);
739         obd_disconnect(sbi->ll_dt_exp);
740         sbi->ll_dt_exp = NULL;
741         /* wait till all OSCs are gone, since cl_cache is accessing sbi.
742          * see LU-2543. */
743         obd_zombie_barrier();
744
745         lprocfs_unregister_mountpoint(sbi);
746
747         obd_fid_fini(sbi->ll_md_exp->exp_obd);
748         obd_disconnect(sbi->ll_md_exp);
749         sbi->ll_md_exp = NULL;
750 }
751
752 void ll_kill_super(struct super_block *sb)
753 {
754         struct ll_sb_info *sbi;
755
756         /* not init sb ?*/
757         if (!(sb->s_flags & MS_ACTIVE))
758                 return;
759
760         sbi = ll_s2sbi(sb);
761         /* we need to restore s_dev from changed for clustered NFS before
762          * put_super because new kernels have cached s_dev and change sb->s_dev
763          * in put_super not affected real removing devices */
764         if (sbi) {
765                 sb->s_dev = sbi->ll_sdev_orig;
766                 sbi->ll_umounting = 1;
767         }
768 }
769
770 static inline int ll_set_opt(const char *opt, char *data, int fl)
771 {
772         if (strncmp(opt, data, strlen(opt)) != 0)
773                 return 0;
774         else
775                 return fl;
776 }
777
778 /* non-client-specific mount options are parsed in lmd_parse */
779 static int ll_options(char *options, int *flags)
780 {
781         int tmp;
782         char *s1 = options, *s2;
783
784         if (!options)
785                 return 0;
786
787         CDEBUG(D_CONFIG, "Parsing opts %s\n", options);
788
789         while (*s1) {
790                 CDEBUG(D_SUPER, "next opt=%s\n", s1);
791                 tmp = ll_set_opt("nolock", s1, LL_SBI_NOLCK);
792                 if (tmp) {
793                         *flags |= tmp;
794                         goto next;
795                 }
796                 tmp = ll_set_opt("flock", s1, LL_SBI_FLOCK);
797                 if (tmp) {
798                         *flags |= tmp;
799                         goto next;
800                 }
801                 tmp = ll_set_opt("localflock", s1, LL_SBI_LOCALFLOCK);
802                 if (tmp) {
803                         *flags |= tmp;
804                         goto next;
805                 }
806                 tmp = ll_set_opt("noflock", s1, LL_SBI_FLOCK|LL_SBI_LOCALFLOCK);
807                 if (tmp) {
808                         *flags &= ~tmp;
809                         goto next;
810                 }
811                 tmp = ll_set_opt("user_xattr", s1, LL_SBI_USER_XATTR);
812                 if (tmp) {
813                         *flags |= tmp;
814                         goto next;
815                 }
816                 tmp = ll_set_opt("nouser_xattr", s1, LL_SBI_USER_XATTR);
817                 if (tmp) {
818                         *flags &= ~tmp;
819                         goto next;
820                 }
821                 tmp = ll_set_opt("remote_client", s1, LL_SBI_RMT_CLIENT);
822                 if (tmp) {
823                         *flags |= tmp;
824                         goto next;
825                 }
826                 tmp = ll_set_opt("user_fid2path", s1, LL_SBI_USER_FID2PATH);
827                 if (tmp) {
828                         *flags |= tmp;
829                         goto next;
830                 }
831                 tmp = ll_set_opt("nouser_fid2path", s1, LL_SBI_USER_FID2PATH);
832                 if (tmp) {
833                         *flags &= ~tmp;
834                         goto next;
835                 }
836
837                 tmp = ll_set_opt("checksum", s1, LL_SBI_CHECKSUM);
838                 if (tmp) {
839                         *flags |= tmp;
840                         goto next;
841                 }
842                 tmp = ll_set_opt("nochecksum", s1, LL_SBI_CHECKSUM);
843                 if (tmp) {
844                         *flags &= ~tmp;
845                         goto next;
846                 }
847                 tmp = ll_set_opt("lruresize", s1, LL_SBI_LRU_RESIZE);
848                 if (tmp) {
849                         *flags |= tmp;
850                         goto next;
851                 }
852                 tmp = ll_set_opt("nolruresize", s1, LL_SBI_LRU_RESIZE);
853                 if (tmp) {
854                         *flags &= ~tmp;
855                         goto next;
856                 }
857                 tmp = ll_set_opt("lazystatfs", s1, LL_SBI_LAZYSTATFS);
858                 if (tmp) {
859                         *flags |= tmp;
860                         goto next;
861                 }
862                 tmp = ll_set_opt("nolazystatfs", s1, LL_SBI_LAZYSTATFS);
863                 if (tmp) {
864                         *flags &= ~tmp;
865                         goto next;
866                 }
867                 tmp = ll_set_opt("som_preview", s1, LL_SBI_SOM_PREVIEW);
868                 if (tmp) {
869                         *flags |= tmp;
870                         goto next;
871                 }
872                 tmp = ll_set_opt("32bitapi", s1, LL_SBI_32BIT_API);
873                 if (tmp) {
874                         *flags |= tmp;
875                         goto next;
876                 }
877                 tmp = ll_set_opt("verbose", s1, LL_SBI_VERBOSE);
878                 if (tmp) {
879                         *flags |= tmp;
880                         goto next;
881                 }
882                 tmp = ll_set_opt("noverbose", s1, LL_SBI_VERBOSE);
883                 if (tmp) {
884                         *flags &= ~tmp;
885                         goto next;
886                 }
887                 LCONSOLE_ERROR_MSG(0x152, "Unknown option '%s', won't mount.\n",
888                                    s1);
889                 return -EINVAL;
890
891 next:
892                 /* Find next opt */
893                 s2 = strchr(s1, ',');
894                 if (s2 == NULL)
895                         break;
896                 s1 = s2 + 1;
897         }
898         return 0;
899 }
900
901 void ll_lli_init(struct ll_inode_info *lli)
902 {
903         lli->lli_inode_magic = LLI_INODE_MAGIC;
904         lli->lli_flags = 0;
905         lli->lli_ioepoch = 0;
906         lli->lli_maxbytes = MAX_LFS_FILESIZE;
907         spin_lock_init(&lli->lli_lock);
908         lli->lli_posix_acl = NULL;
909         lli->lli_remote_perms = NULL;
910         mutex_init(&lli->lli_rmtperm_mutex);
911         /* Do not set lli_fid, it has been initialized already. */
912         fid_zero(&lli->lli_pfid);
913         INIT_LIST_HEAD(&lli->lli_close_list);
914         INIT_LIST_HEAD(&lli->lli_oss_capas);
915         atomic_set(&lli->lli_open_count, 0);
916         lli->lli_mds_capa = NULL;
917         lli->lli_rmtperm_time = 0;
918         lli->lli_pending_och = NULL;
919         lli->lli_mds_read_och = NULL;
920         lli->lli_mds_write_och = NULL;
921         lli->lli_mds_exec_och = NULL;
922         lli->lli_open_fd_read_count = 0;
923         lli->lli_open_fd_write_count = 0;
924         lli->lli_open_fd_exec_count = 0;
925         mutex_init(&lli->lli_och_mutex);
926         spin_lock_init(&lli->lli_agl_lock);
927         lli->lli_has_smd = false;
928         spin_lock_init(&lli->lli_layout_lock);
929         ll_layout_version_set(lli, LL_LAYOUT_GEN_NONE);
930         lli->lli_clob = NULL;
931
932         init_rwsem(&lli->lli_xattrs_list_rwsem);
933         mutex_init(&lli->lli_xattrs_enq_lock);
934
935         LASSERT(lli->lli_vfs_inode.i_mode != 0);
936         if (S_ISDIR(lli->lli_vfs_inode.i_mode)) {
937                 mutex_init(&lli->lli_readdir_mutex);
938                 lli->lli_opendir_key = NULL;
939                 lli->lli_sai = NULL;
940                 spin_lock_init(&lli->lli_sa_lock);
941                 lli->lli_opendir_pid = 0;
942         } else {
943                 mutex_init(&lli->lli_size_mutex);
944                 lli->lli_symlink_name = NULL;
945                 init_rwsem(&lli->lli_trunc_sem);
946                 mutex_init(&lli->lli_write_mutex);
947                 init_rwsem(&lli->lli_glimpse_sem);
948                 lli->lli_glimpse_time = 0;
949                 INIT_LIST_HEAD(&lli->lli_agl_list);
950                 lli->lli_agl_index = 0;
951                 lli->lli_async_rc = 0;
952         }
953         mutex_init(&lli->lli_layout_mutex);
954 }
955
956 static inline int ll_bdi_register(struct backing_dev_info *bdi)
957 {
958         static atomic_t ll_bdi_num = ATOMIC_INIT(0);
959
960         bdi->name = "lustre";
961         return bdi_register(bdi, NULL, "lustre-%d",
962                             atomic_inc_return(&ll_bdi_num));
963 }
964
965 int ll_fill_super(struct super_block *sb, struct vfsmount *mnt)
966 {
967         struct lustre_profile *lprof = NULL;
968         struct lustre_sb_info *lsi = s2lsi(sb);
969         struct ll_sb_info *sbi;
970         char  *dt = NULL, *md = NULL;
971         char  *profilenm = get_profile_name(sb);
972         struct config_llog_instance *cfg;
973         /* %p for void* in printf needs 16+2 characters: 0xffffffffffffffff */
974         const int instlen = sizeof(cfg->cfg_instance) * 2 + 2;
975         int    err;
976
977         CDEBUG(D_VFSTRACE, "VFS Op: sb %p\n", sb);
978
979         cfg = kzalloc(sizeof(*cfg), GFP_NOFS);
980         if (!cfg)
981                 return -ENOMEM;
982
983         try_module_get(THIS_MODULE);
984
985         /* client additional sb info */
986         lsi->lsi_llsbi = sbi = ll_init_sbi();
987         if (!sbi) {
988                 module_put(THIS_MODULE);
989                 OBD_FREE_PTR(cfg);
990                 return -ENOMEM;
991         }
992
993         err = ll_options(lsi->lsi_lmd->lmd_opts, &sbi->ll_flags);
994         if (err)
995                 goto out_free;
996
997         err = bdi_init(&lsi->lsi_bdi);
998         if (err)
999                 goto out_free;
1000         lsi->lsi_flags |= LSI_BDI_INITIALIZED;
1001         lsi->lsi_bdi.capabilities = BDI_CAP_MAP_COPY;
1002         err = ll_bdi_register(&lsi->lsi_bdi);
1003         if (err)
1004                 goto out_free;
1005
1006         sb->s_bdi = &lsi->lsi_bdi;
1007         /* kernel >= 2.6.38 store dentry operations in sb->s_d_op. */
1008         sb->s_d_op = &ll_d_ops;
1009
1010         /* Generate a string unique to this super, in case some joker tries
1011            to mount the same fs at two mount points.
1012            Use the address of the super itself.*/
1013         cfg->cfg_instance = sb;
1014         cfg->cfg_uuid = lsi->lsi_llsbi->ll_sb_uuid;
1015         cfg->cfg_callback = class_config_llog_handler;
1016         /* set up client obds */
1017         err = lustre_process_log(sb, profilenm, cfg);
1018         if (err < 0) {
1019                 CERROR("Unable to process log: %d\n", err);
1020                 goto out_free;
1021         }
1022
1023         /* Profile set with LCFG_MOUNTOPT so we can find our mdc and osc obds */
1024         lprof = class_get_profile(profilenm);
1025         if (lprof == NULL) {
1026                 LCONSOLE_ERROR_MSG(0x156, "The client profile '%s' could not be"
1027                                    " read from the MGS.  Does that filesystem "
1028                                    "exist?\n", profilenm);
1029                 err = -EINVAL;
1030                 goto out_free;
1031         }
1032         CDEBUG(D_CONFIG, "Found profile %s: mdc=%s osc=%s\n", profilenm,
1033                lprof->lp_md, lprof->lp_dt);
1034
1035         dt = kzalloc(strlen(lprof->lp_dt) + instlen + 2, GFP_NOFS);
1036         if (!dt) {
1037                 err = -ENOMEM;
1038                 goto out_free;
1039         }
1040         sprintf(dt, "%s-%p", lprof->lp_dt, cfg->cfg_instance);
1041
1042         md = kzalloc(strlen(lprof->lp_md) + instlen + 2, GFP_NOFS);
1043         if (!md) {
1044                 err = -ENOMEM;
1045                 goto out_free;
1046         }
1047         sprintf(md, "%s-%p", lprof->lp_md, cfg->cfg_instance);
1048
1049         /* connections, registrations, sb setup */
1050         err = client_common_fill_super(sb, md, dt, mnt);
1051
1052 out_free:
1053         if (md)
1054                 OBD_FREE(md, strlen(lprof->lp_md) + instlen + 2);
1055         if (dt)
1056                 OBD_FREE(dt, strlen(lprof->lp_dt) + instlen + 2);
1057         if (err)
1058                 ll_put_super(sb);
1059         else if (sbi->ll_flags & LL_SBI_VERBOSE)
1060                 LCONSOLE_WARN("Mounted %s\n", profilenm);
1061
1062         OBD_FREE_PTR(cfg);
1063         return err;
1064 } /* ll_fill_super */
1065
1066 void ll_put_super(struct super_block *sb)
1067 {
1068         struct config_llog_instance cfg, params_cfg;
1069         struct obd_device *obd;
1070         struct lustre_sb_info *lsi = s2lsi(sb);
1071         struct ll_sb_info *sbi = ll_s2sbi(sb);
1072         char *profilenm = get_profile_name(sb);
1073         int next, force = 1;
1074
1075         CDEBUG(D_VFSTRACE, "VFS Op: sb %p - %s\n", sb, profilenm);
1076
1077         ll_print_capa_stat(sbi);
1078
1079         cfg.cfg_instance = sb;
1080         lustre_end_log(sb, profilenm, &cfg);
1081
1082         params_cfg.cfg_instance = sb;
1083         lustre_end_log(sb, PARAMS_FILENAME, &params_cfg);
1084
1085         if (sbi->ll_md_exp) {
1086                 obd = class_exp2obd(sbi->ll_md_exp);
1087                 if (obd)
1088                         force = obd->obd_force;
1089         }
1090
1091         /* We need to set force before the lov_disconnect in
1092            lustre_common_put_super, since l_d cleans up osc's as well. */
1093         if (force) {
1094                 next = 0;
1095                 while ((obd = class_devices_in_group(&sbi->ll_sb_uuid,
1096                                                      &next)) != NULL) {
1097                         obd->obd_force = force;
1098                 }
1099         }
1100
1101         if (sbi->ll_lcq) {
1102                 /* Only if client_common_fill_super succeeded */
1103                 client_common_put_super(sb);
1104         }
1105
1106         next = 0;
1107         while ((obd = class_devices_in_group(&sbi->ll_sb_uuid, &next)))
1108                 class_manual_cleanup(obd);
1109
1110         if (sbi->ll_flags & LL_SBI_VERBOSE)
1111                 LCONSOLE_WARN("Unmounted %s\n", profilenm ? profilenm : "");
1112
1113         if (profilenm)
1114                 class_del_profile(profilenm);
1115
1116         if (lsi->lsi_flags & LSI_BDI_INITIALIZED) {
1117                 bdi_destroy(&lsi->lsi_bdi);
1118                 lsi->lsi_flags &= ~LSI_BDI_INITIALIZED;
1119         }
1120
1121         ll_free_sbi(sb);
1122         lsi->lsi_llsbi = NULL;
1123
1124         lustre_common_put_super(sb);
1125
1126         module_put(THIS_MODULE);
1127 } /* client_put_super */
1128
1129 struct inode *ll_inode_from_resource_lock(struct ldlm_lock *lock)
1130 {
1131         struct inode *inode = NULL;
1132
1133         /* NOTE: we depend on atomic igrab() -bzzz */
1134         lock_res_and_lock(lock);
1135         if (lock->l_resource->lr_lvb_inode) {
1136                 struct ll_inode_info *lli;
1137
1138                 lli = ll_i2info(lock->l_resource->lr_lvb_inode);
1139                 if (lli->lli_inode_magic == LLI_INODE_MAGIC) {
1140                         inode = igrab(lock->l_resource->lr_lvb_inode);
1141                 } else {
1142                         inode = lock->l_resource->lr_lvb_inode;
1143                         LDLM_DEBUG_LIMIT(inode->i_state & I_FREEING ?  D_INFO :
1144                                          D_WARNING, lock, "lr_lvb_inode %p is "
1145                                          "bogus: magic %08x",
1146                                          lock->l_resource->lr_lvb_inode,
1147                                          lli->lli_inode_magic);
1148                         inode = NULL;
1149                 }
1150         }
1151         unlock_res_and_lock(lock);
1152         return inode;
1153 }
1154
1155 void ll_clear_inode(struct inode *inode)
1156 {
1157         struct ll_inode_info *lli = ll_i2info(inode);
1158         struct ll_sb_info *sbi = ll_i2sbi(inode);
1159
1160         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
1161                inode->i_generation, inode);
1162
1163         if (S_ISDIR(inode->i_mode)) {
1164                 /* these should have been cleared in ll_file_release */
1165                 LASSERT(lli->lli_opendir_key == NULL);
1166                 LASSERT(lli->lli_sai == NULL);
1167                 LASSERT(lli->lli_opendir_pid == 0);
1168         }
1169
1170         spin_lock(&lli->lli_lock);
1171         ll_i2info(inode)->lli_flags &= ~LLIF_MDS_SIZE_LOCK;
1172         spin_unlock(&lli->lli_lock);
1173         md_null_inode(sbi->ll_md_exp, ll_inode2fid(inode));
1174
1175         LASSERT(!lli->lli_open_fd_write_count);
1176         LASSERT(!lli->lli_open_fd_read_count);
1177         LASSERT(!lli->lli_open_fd_exec_count);
1178
1179         if (lli->lli_mds_write_och)
1180                 ll_md_real_close(inode, FMODE_WRITE);
1181         if (lli->lli_mds_exec_och)
1182                 ll_md_real_close(inode, FMODE_EXEC);
1183         if (lli->lli_mds_read_och)
1184                 ll_md_real_close(inode, FMODE_READ);
1185
1186         if (S_ISLNK(inode->i_mode) && lli->lli_symlink_name) {
1187                 OBD_FREE(lli->lli_symlink_name,
1188                          strlen(lli->lli_symlink_name) + 1);
1189                 lli->lli_symlink_name = NULL;
1190         }
1191
1192         ll_xattr_cache_destroy(inode);
1193
1194         if (sbi->ll_flags & LL_SBI_RMT_CLIENT) {
1195                 LASSERT(lli->lli_posix_acl == NULL);
1196                 if (lli->lli_remote_perms) {
1197                         free_rmtperm_hash(lli->lli_remote_perms);
1198                         lli->lli_remote_perms = NULL;
1199                 }
1200         }
1201 #ifdef CONFIG_FS_POSIX_ACL
1202         else if (lli->lli_posix_acl) {
1203                 LASSERT(atomic_read(&lli->lli_posix_acl->a_refcount) == 1);
1204                 LASSERT(lli->lli_remote_perms == NULL);
1205                 posix_acl_release(lli->lli_posix_acl);
1206                 lli->lli_posix_acl = NULL;
1207         }
1208 #endif
1209         lli->lli_inode_magic = LLI_INODE_DEAD;
1210
1211         ll_clear_inode_capas(inode);
1212         if (!S_ISDIR(inode->i_mode))
1213                 LASSERT(list_empty(&lli->lli_agl_list));
1214
1215         /*
1216          * XXX This has to be done before lsm is freed below, because
1217          * cl_object still uses inode lsm.
1218          */
1219         cl_inode_fini(inode);
1220         lli->lli_has_smd = false;
1221 }
1222
1223 static int ll_md_setattr(struct dentry *dentry, struct md_op_data *op_data,
1224                   struct md_open_data **mod)
1225 {
1226         struct lustre_md md;
1227         struct inode *inode = dentry->d_inode;
1228         struct ll_sb_info *sbi = ll_i2sbi(inode);
1229         struct ptlrpc_request *request = NULL;
1230         int rc, ia_valid;
1231
1232         op_data = ll_prep_md_op_data(op_data, inode, NULL, NULL, 0, 0,
1233                                      LUSTRE_OPC_ANY, NULL);
1234         if (IS_ERR(op_data))
1235                 return PTR_ERR(op_data);
1236
1237         rc = md_setattr(sbi->ll_md_exp, op_data, NULL, 0, NULL, 0,
1238                         &request, mod);
1239         if (rc) {
1240                 ptlrpc_req_finished(request);
1241                 if (rc == -ENOENT) {
1242                         clear_nlink(inode);
1243                         /* Unlinked special device node? Or just a race?
1244                          * Pretend we done everything. */
1245                         if (!S_ISREG(inode->i_mode) &&
1246                             !S_ISDIR(inode->i_mode)) {
1247                                 ia_valid = op_data->op_attr.ia_valid;
1248                                 op_data->op_attr.ia_valid &= ~TIMES_SET_FLAGS;
1249                                 rc = simple_setattr(dentry, &op_data->op_attr);
1250                                 op_data->op_attr.ia_valid = ia_valid;
1251                         }
1252                 } else if (rc != -EPERM && rc != -EACCES && rc != -ETXTBSY) {
1253                         CERROR("md_setattr fails: rc = %d\n", rc);
1254                 }
1255                 return rc;
1256         }
1257
1258         rc = md_get_lustre_md(sbi->ll_md_exp, request, sbi->ll_dt_exp,
1259                               sbi->ll_md_exp, &md);
1260         if (rc) {
1261                 ptlrpc_req_finished(request);
1262                 return rc;
1263         }
1264
1265         ia_valid = op_data->op_attr.ia_valid;
1266         /* inode size will be in ll_setattr_ost, can't do it now since dirty
1267          * cache is not cleared yet. */
1268         op_data->op_attr.ia_valid &= ~(TIMES_SET_FLAGS | ATTR_SIZE);
1269         rc = simple_setattr(dentry, &op_data->op_attr);
1270         op_data->op_attr.ia_valid = ia_valid;
1271
1272         /* Extract epoch data if obtained. */
1273         op_data->op_handle = md.body->handle;
1274         op_data->op_ioepoch = md.body->ioepoch;
1275
1276         ll_update_inode(inode, &md);
1277         ptlrpc_req_finished(request);
1278
1279         return rc;
1280 }
1281
1282 /* Close IO epoch and send Size-on-MDS attribute update. */
1283 static int ll_setattr_done_writing(struct inode *inode,
1284                                    struct md_op_data *op_data,
1285                                    struct md_open_data *mod)
1286 {
1287         struct ll_inode_info *lli = ll_i2info(inode);
1288         int rc = 0;
1289
1290         LASSERT(op_data != NULL);
1291         if (!S_ISREG(inode->i_mode))
1292                 return 0;
1293
1294         CDEBUG(D_INODE, "Epoch %llu closed on "DFID" for truncate\n",
1295                op_data->op_ioepoch, PFID(&lli->lli_fid));
1296
1297         op_data->op_flags = MF_EPOCH_CLOSE;
1298         ll_done_writing_attr(inode, op_data);
1299         ll_pack_inode2opdata(inode, op_data, NULL);
1300
1301         rc = md_done_writing(ll_i2sbi(inode)->ll_md_exp, op_data, mod);
1302         if (rc == -EAGAIN) {
1303                 /* MDS has instructed us to obtain Size-on-MDS attribute
1304                  * from OSTs and send setattr to back to MDS. */
1305                 rc = ll_som_update(inode, op_data);
1306         } else if (rc) {
1307                 CERROR("inode %lu mdc truncate failed: rc = %d\n",
1308                        inode->i_ino, rc);
1309         }
1310         return rc;
1311 }
1312
1313 static int ll_setattr_ost(struct inode *inode, struct iattr *attr)
1314 {
1315         struct obd_capa *capa;
1316         int rc;
1317
1318         if (attr->ia_valid & ATTR_SIZE)
1319                 capa = ll_osscapa_get(inode, CAPA_OPC_OSS_TRUNC);
1320         else
1321                 capa = ll_mdscapa_get(inode);
1322
1323         rc = cl_setattr_ost(inode, attr, capa);
1324
1325         if (attr->ia_valid & ATTR_SIZE)
1326                 ll_truncate_free_capa(capa);
1327         else
1328                 capa_put(capa);
1329
1330         return rc;
1331 }
1332
1333
1334 /* If this inode has objects allocated to it (lsm != NULL), then the OST
1335  * object(s) determine the file size and mtime.  Otherwise, the MDS will
1336  * keep these values until such a time that objects are allocated for it.
1337  * We do the MDS operations first, as it is checking permissions for us.
1338  * We don't to the MDS RPC if there is nothing that we want to store there,
1339  * otherwise there is no harm in updating mtime/atime on the MDS if we are
1340  * going to do an RPC anyways.
1341  *
1342  * If we are doing a truncate, we will send the mtime and ctime updates
1343  * to the OST with the punch RPC, otherwise we do an explicit setattr RPC.
1344  * I don't believe it is possible to get e.g. ATTR_MTIME_SET and ATTR_SIZE
1345  * at the same time.
1346  *
1347  * In case of HSMimport, we only set attr on MDS.
1348  */
1349 int ll_setattr_raw(struct dentry *dentry, struct iattr *attr, bool hsm_import)
1350 {
1351         struct inode *inode = dentry->d_inode;
1352         struct ll_inode_info *lli = ll_i2info(inode);
1353         struct md_op_data *op_data = NULL;
1354         struct md_open_data *mod = NULL;
1355         bool file_is_released = false;
1356         int rc = 0, rc1 = 0;
1357
1358         CDEBUG(D_VFSTRACE,
1359                 "%s: setattr inode %p/fid:"DFID
1360                 " from %llu to %llu, valid %x, hsm_import %d\n",
1361                 ll_get_fsname(inode->i_sb, NULL, 0), inode,
1362                 PFID(&lli->lli_fid), i_size_read(inode), attr->ia_size,
1363                 attr->ia_valid, hsm_import);
1364
1365         if (attr->ia_valid & ATTR_SIZE) {
1366                 /* Check new size against VFS/VM file size limit and rlimit */
1367                 rc = inode_newsize_ok(inode, attr->ia_size);
1368                 if (rc)
1369                         return rc;
1370
1371                 /* The maximum Lustre file size is variable, based on the
1372                  * OST maximum object size and number of stripes.  This
1373                  * needs another check in addition to the VFS check above. */
1374                 if (attr->ia_size > ll_file_maxbytes(inode)) {
1375                         CDEBUG(D_INODE, "file "DFID" too large %llu > %llu\n",
1376                                PFID(&lli->lli_fid), attr->ia_size,
1377                                ll_file_maxbytes(inode));
1378                         return -EFBIG;
1379                 }
1380
1381                 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
1382         }
1383
1384         /* POSIX: check before ATTR_*TIME_SET set (from inode_change_ok) */
1385         if (attr->ia_valid & TIMES_SET_FLAGS) {
1386                 if ((!uid_eq(current_fsuid(), inode->i_uid)) &&
1387                     !capable(CFS_CAP_FOWNER))
1388                         return -EPERM;
1389         }
1390
1391         /* We mark all of the fields "set" so MDS/OST does not re-set them */
1392         if (attr->ia_valid & ATTR_CTIME) {
1393                 attr->ia_ctime = CURRENT_TIME;
1394                 attr->ia_valid |= ATTR_CTIME_SET;
1395         }
1396         if (!(attr->ia_valid & ATTR_ATIME_SET) &&
1397             (attr->ia_valid & ATTR_ATIME)) {
1398                 attr->ia_atime = CURRENT_TIME;
1399                 attr->ia_valid |= ATTR_ATIME_SET;
1400         }
1401         if (!(attr->ia_valid & ATTR_MTIME_SET) &&
1402             (attr->ia_valid & ATTR_MTIME)) {
1403                 attr->ia_mtime = CURRENT_TIME;
1404                 attr->ia_valid |= ATTR_MTIME_SET;
1405         }
1406
1407         if (attr->ia_valid & (ATTR_MTIME | ATTR_CTIME))
1408                 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu, now = %lu\n",
1409                        LTIME_S(attr->ia_mtime), LTIME_S(attr->ia_ctime),
1410                        get_seconds());
1411
1412         /* If we are changing file size, file content is modified, flag it. */
1413         if (attr->ia_valid & ATTR_SIZE) {
1414                 attr->ia_valid |= MDS_OPEN_OWNEROVERRIDE;
1415                 spin_lock(&lli->lli_lock);
1416                 lli->lli_flags |= LLIF_DATA_MODIFIED;
1417                 spin_unlock(&lli->lli_lock);
1418         }
1419
1420         /* We always do an MDS RPC, even if we're only changing the size;
1421          * only the MDS knows whether truncate() should fail with -ETXTBUSY */
1422
1423         op_data = kzalloc(sizeof(*op_data), GFP_NOFS);
1424         if (!op_data)
1425                 return -ENOMEM;
1426
1427         if (!S_ISDIR(inode->i_mode)) {
1428                 if (attr->ia_valid & ATTR_SIZE)
1429                         inode_dio_write_done(inode);
1430                 mutex_unlock(&inode->i_mutex);
1431         }
1432
1433         memcpy(&op_data->op_attr, attr, sizeof(*attr));
1434
1435         /* Open epoch for truncate. */
1436         if (exp_connect_som(ll_i2mdexp(inode)) &&
1437             (attr->ia_valid & (ATTR_SIZE | ATTR_MTIME | ATTR_MTIME_SET)))
1438                 op_data->op_flags = MF_EPOCH_OPEN;
1439
1440         /* truncate on a released file must failed with -ENODATA,
1441          * so size must not be set on MDS for released file
1442          * but other attributes must be set
1443          */
1444         if (S_ISREG(inode->i_mode)) {
1445                 struct lov_stripe_md *lsm;
1446                 __u32 gen;
1447
1448                 ll_layout_refresh(inode, &gen);
1449                 lsm = ccc_inode_lsm_get(inode);
1450                 if (lsm && lsm->lsm_pattern & LOV_PATTERN_F_RELEASED)
1451                         file_is_released = true;
1452                 ccc_inode_lsm_put(inode, lsm);
1453         }
1454
1455         /* if not in HSM import mode, clear size attr for released file
1456          * we clear the attribute send to MDT in op_data, not the original
1457          * received from caller in attr which is used later to
1458          * decide return code */
1459         if (file_is_released && (attr->ia_valid & ATTR_SIZE) && !hsm_import)
1460                 op_data->op_attr.ia_valid &= ~ATTR_SIZE;
1461
1462         rc = ll_md_setattr(dentry, op_data, &mod);
1463         if (rc)
1464                 goto out;
1465
1466         /* truncate failed (only when non HSM import), others succeed */
1467         if (file_is_released) {
1468                 if ((attr->ia_valid & ATTR_SIZE) && !hsm_import)
1469                         rc = -ENODATA;
1470                 else
1471                         rc = 0;
1472                 goto out;
1473         }
1474
1475         /* RPC to MDT is sent, cancel data modification flag */
1476         if (rc == 0 && (op_data->op_bias & MDS_DATA_MODIFIED)) {
1477                 spin_lock(&lli->lli_lock);
1478                 lli->lli_flags &= ~LLIF_DATA_MODIFIED;
1479                 spin_unlock(&lli->lli_lock);
1480         }
1481
1482         ll_ioepoch_open(lli, op_data->op_ioepoch);
1483         if (!S_ISREG(inode->i_mode)) {
1484                 rc = 0;
1485                 goto out;
1486         }
1487
1488         if (attr->ia_valid & (ATTR_SIZE |
1489                               ATTR_ATIME | ATTR_ATIME_SET |
1490                               ATTR_MTIME | ATTR_MTIME_SET))
1491                 /* For truncate and utimes sending attributes to OSTs, setting
1492                  * mtime/atime to the past will be performed under PW [0:EOF]
1493                  * extent lock (new_size:EOF for truncate).  It may seem
1494                  * excessive to send mtime/atime updates to OSTs when not
1495                  * setting times to past, but it is necessary due to possible
1496                  * time de-synchronization between MDT inode and OST objects */
1497                 if (attr->ia_valid & ATTR_SIZE)
1498                         down_write(&lli->lli_trunc_sem);
1499                 rc = ll_setattr_ost(inode, attr);
1500                 if (attr->ia_valid & ATTR_SIZE)
1501                         up_write(&lli->lli_trunc_sem);
1502 out:
1503         if (op_data) {
1504                 if (op_data->op_ioepoch) {
1505                         rc1 = ll_setattr_done_writing(inode, op_data, mod);
1506                         if (!rc)
1507                                 rc = rc1;
1508                 }
1509                 ll_finish_md_op_data(op_data);
1510         }
1511         if (!S_ISDIR(inode->i_mode)) {
1512                 mutex_lock(&inode->i_mutex);
1513                 if ((attr->ia_valid & ATTR_SIZE) && !hsm_import)
1514                         inode_dio_wait(inode);
1515         }
1516
1517         ll_stats_ops_tally(ll_i2sbi(inode), (attr->ia_valid & ATTR_SIZE) ?
1518                         LPROC_LL_TRUNC : LPROC_LL_SETATTR, 1);
1519
1520         return rc;
1521 }
1522
1523 int ll_setattr(struct dentry *de, struct iattr *attr)
1524 {
1525         int mode = de->d_inode->i_mode;
1526
1527         if ((attr->ia_valid & (ATTR_CTIME|ATTR_SIZE|ATTR_MODE)) ==
1528                               (ATTR_CTIME|ATTR_SIZE|ATTR_MODE))
1529                 attr->ia_valid |= MDS_OPEN_OWNEROVERRIDE;
1530
1531         if (((attr->ia_valid & (ATTR_MODE|ATTR_FORCE|ATTR_SIZE)) ==
1532                                (ATTR_SIZE|ATTR_MODE)) &&
1533             (((mode & S_ISUID) && !(attr->ia_mode & S_ISUID)) ||
1534              (((mode & (S_ISGID|S_IXGRP)) == (S_ISGID|S_IXGRP)) &&
1535               !(attr->ia_mode & S_ISGID))))
1536                 attr->ia_valid |= ATTR_FORCE;
1537
1538         if ((attr->ia_valid & ATTR_MODE) &&
1539             (mode & S_ISUID) &&
1540             !(attr->ia_mode & S_ISUID) &&
1541             !(attr->ia_valid & ATTR_KILL_SUID))
1542                 attr->ia_valid |= ATTR_KILL_SUID;
1543
1544         if ((attr->ia_valid & ATTR_MODE) &&
1545             ((mode & (S_ISGID|S_IXGRP)) == (S_ISGID|S_IXGRP)) &&
1546             !(attr->ia_mode & S_ISGID) &&
1547             !(attr->ia_valid & ATTR_KILL_SGID))
1548                 attr->ia_valid |= ATTR_KILL_SGID;
1549
1550         return ll_setattr_raw(de, attr, false);
1551 }
1552
1553 int ll_statfs_internal(struct super_block *sb, struct obd_statfs *osfs,
1554                        __u64 max_age, __u32 flags)
1555 {
1556         struct ll_sb_info *sbi = ll_s2sbi(sb);
1557         struct obd_statfs obd_osfs;
1558         int rc;
1559
1560         rc = obd_statfs(NULL, sbi->ll_md_exp, osfs, max_age, flags);
1561         if (rc) {
1562                 CERROR("md_statfs fails: rc = %d\n", rc);
1563                 return rc;
1564         }
1565
1566         osfs->os_type = sb->s_magic;
1567
1568         CDEBUG(D_SUPER, "MDC blocks %llu/%llu objects %llu/%llu\n",
1569                osfs->os_bavail, osfs->os_blocks, osfs->os_ffree,
1570                osfs->os_files);
1571
1572         if (sbi->ll_flags & LL_SBI_LAZYSTATFS)
1573                 flags |= OBD_STATFS_NODELAY;
1574
1575         rc = obd_statfs_rqset(sbi->ll_dt_exp, &obd_osfs, max_age, flags);
1576         if (rc) {
1577                 CERROR("obd_statfs fails: rc = %d\n", rc);
1578                 return rc;
1579         }
1580
1581         CDEBUG(D_SUPER, "OSC blocks %llu/%llu objects %llu/%llu\n",
1582                obd_osfs.os_bavail, obd_osfs.os_blocks, obd_osfs.os_ffree,
1583                obd_osfs.os_files);
1584
1585         osfs->os_bsize = obd_osfs.os_bsize;
1586         osfs->os_blocks = obd_osfs.os_blocks;
1587         osfs->os_bfree = obd_osfs.os_bfree;
1588         osfs->os_bavail = obd_osfs.os_bavail;
1589
1590         /* If we don't have as many objects free on the OST as inodes
1591          * on the MDS, we reduce the total number of inodes to
1592          * compensate, so that the "inodes in use" number is correct.
1593          */
1594         if (obd_osfs.os_ffree < osfs->os_ffree) {
1595                 osfs->os_files = (osfs->os_files - osfs->os_ffree) +
1596                         obd_osfs.os_ffree;
1597                 osfs->os_ffree = obd_osfs.os_ffree;
1598         }
1599
1600         return rc;
1601 }
1602 int ll_statfs(struct dentry *de, struct kstatfs *sfs)
1603 {
1604         struct super_block *sb = de->d_sb;
1605         struct obd_statfs osfs;
1606         int rc;
1607
1608         CDEBUG(D_VFSTRACE, "VFS Op: at %llu jiffies\n", get_jiffies_64());
1609         ll_stats_ops_tally(ll_s2sbi(sb), LPROC_LL_STAFS, 1);
1610
1611         /* Some amount of caching on the client is allowed */
1612         rc = ll_statfs_internal(sb, &osfs,
1613                                 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
1614                                 0);
1615         if (rc)
1616                 return rc;
1617
1618         statfs_unpack(sfs, &osfs);
1619
1620         /* We need to downshift for all 32-bit kernels, because we can't
1621          * tell if the kernel is being called via sys_statfs64() or not.
1622          * Stop before overflowing f_bsize - in which case it is better
1623          * to just risk EOVERFLOW if caller is using old sys_statfs(). */
1624         if (sizeof(long) < 8) {
1625                 while (osfs.os_blocks > ~0UL && sfs->f_bsize < 0x40000000) {
1626                         sfs->f_bsize <<= 1;
1627
1628                         osfs.os_blocks >>= 1;
1629                         osfs.os_bfree >>= 1;
1630                         osfs.os_bavail >>= 1;
1631                 }
1632         }
1633
1634         sfs->f_blocks = osfs.os_blocks;
1635         sfs->f_bfree = osfs.os_bfree;
1636         sfs->f_bavail = osfs.os_bavail;
1637         sfs->f_fsid = ll_s2sbi(sb)->ll_fsid;
1638         return 0;
1639 }
1640
1641 void ll_inode_size_lock(struct inode *inode)
1642 {
1643         struct ll_inode_info *lli;
1644
1645         LASSERT(!S_ISDIR(inode->i_mode));
1646
1647         lli = ll_i2info(inode);
1648         mutex_lock(&lli->lli_size_mutex);
1649 }
1650
1651 void ll_inode_size_unlock(struct inode *inode)
1652 {
1653         struct ll_inode_info *lli;
1654
1655         lli = ll_i2info(inode);
1656         mutex_unlock(&lli->lli_size_mutex);
1657 }
1658
1659 void ll_update_inode(struct inode *inode, struct lustre_md *md)
1660 {
1661         struct ll_inode_info *lli = ll_i2info(inode);
1662         struct mdt_body *body = md->body;
1663         struct lov_stripe_md *lsm = md->lsm;
1664         struct ll_sb_info *sbi = ll_i2sbi(inode);
1665
1666         LASSERT ((lsm != NULL) == ((body->valid & OBD_MD_FLEASIZE) != 0));
1667         if (lsm != NULL) {
1668                 if (!lli->lli_has_smd &&
1669                     !(sbi->ll_flags & LL_SBI_LAYOUT_LOCK))
1670                         cl_file_inode_init(inode, md);
1671
1672                 lli->lli_maxbytes = lsm->lsm_maxbytes;
1673                 if (lli->lli_maxbytes > MAX_LFS_FILESIZE)
1674                         lli->lli_maxbytes = MAX_LFS_FILESIZE;
1675         }
1676
1677         if (sbi->ll_flags & LL_SBI_RMT_CLIENT) {
1678                 if (body->valid & OBD_MD_FLRMTPERM)
1679                         ll_update_remote_perm(inode, md->remote_perm);
1680         }
1681 #ifdef CONFIG_FS_POSIX_ACL
1682         else if (body->valid & OBD_MD_FLACL) {
1683                 spin_lock(&lli->lli_lock);
1684                 if (lli->lli_posix_acl)
1685                         posix_acl_release(lli->lli_posix_acl);
1686                 lli->lli_posix_acl = md->posix_acl;
1687                 spin_unlock(&lli->lli_lock);
1688         }
1689 #endif
1690         inode->i_ino = cl_fid_build_ino(&body->fid1,
1691                                         sbi->ll_flags & LL_SBI_32BIT_API);
1692         inode->i_generation = cl_fid_build_gen(&body->fid1);
1693
1694         if (body->valid & OBD_MD_FLATIME) {
1695                 if (body->atime > LTIME_S(inode->i_atime))
1696                         LTIME_S(inode->i_atime) = body->atime;
1697                 lli->lli_lvb.lvb_atime = body->atime;
1698         }
1699         if (body->valid & OBD_MD_FLMTIME) {
1700                 if (body->mtime > LTIME_S(inode->i_mtime)) {
1701                         CDEBUG(D_INODE, "setting ino %lu mtime from %lu to %llu\n",
1702                                inode->i_ino, LTIME_S(inode->i_mtime),
1703                                body->mtime);
1704                         LTIME_S(inode->i_mtime) = body->mtime;
1705                 }
1706                 lli->lli_lvb.lvb_mtime = body->mtime;
1707         }
1708         if (body->valid & OBD_MD_FLCTIME) {
1709                 if (body->ctime > LTIME_S(inode->i_ctime))
1710                         LTIME_S(inode->i_ctime) = body->ctime;
1711                 lli->lli_lvb.lvb_ctime = body->ctime;
1712         }
1713         if (body->valid & OBD_MD_FLMODE)
1714                 inode->i_mode = (inode->i_mode & S_IFMT)|(body->mode & ~S_IFMT);
1715         if (body->valid & OBD_MD_FLTYPE)
1716                 inode->i_mode = (inode->i_mode & ~S_IFMT)|(body->mode & S_IFMT);
1717         LASSERT(inode->i_mode != 0);
1718         if (S_ISREG(inode->i_mode))
1719                 inode->i_blkbits = min(PTLRPC_MAX_BRW_BITS + 1,
1720                                        LL_MAX_BLKSIZE_BITS);
1721         else
1722                 inode->i_blkbits = inode->i_sb->s_blocksize_bits;
1723         if (body->valid & OBD_MD_FLUID)
1724                 inode->i_uid = make_kuid(&init_user_ns, body->uid);
1725         if (body->valid & OBD_MD_FLGID)
1726                 inode->i_gid = make_kgid(&init_user_ns, body->gid);
1727         if (body->valid & OBD_MD_FLFLAGS)
1728                 inode->i_flags = ll_ext_to_inode_flags(body->flags);
1729         if (body->valid & OBD_MD_FLNLINK)
1730                 set_nlink(inode, body->nlink);
1731         if (body->valid & OBD_MD_FLRDEV)
1732                 inode->i_rdev = old_decode_dev(body->rdev);
1733
1734         if (body->valid & OBD_MD_FLID) {
1735                 /* FID shouldn't be changed! */
1736                 if (fid_is_sane(&lli->lli_fid)) {
1737                         LASSERTF(lu_fid_eq(&lli->lli_fid, &body->fid1),
1738                                  "Trying to change FID "DFID
1739                                  " to the "DFID", inode %lu/%u(%p)\n",
1740                                  PFID(&lli->lli_fid), PFID(&body->fid1),
1741                                  inode->i_ino, inode->i_generation, inode);
1742                 } else
1743                         lli->lli_fid = body->fid1;
1744         }
1745
1746         LASSERT(fid_seq(&lli->lli_fid) != 0);
1747
1748         if (body->valid & OBD_MD_FLSIZE) {
1749                 if (exp_connect_som(ll_i2mdexp(inode)) &&
1750                     S_ISREG(inode->i_mode)) {
1751                         struct lustre_handle lockh;
1752                         ldlm_mode_t mode;
1753
1754                         /* As it is possible a blocking ast has been processed
1755                          * by this time, we need to check there is an UPDATE
1756                          * lock on the client and set LLIF_MDS_SIZE_LOCK holding
1757                          * it. */
1758                         mode = ll_take_md_lock(inode, MDS_INODELOCK_UPDATE,
1759                                                &lockh, LDLM_FL_CBPENDING,
1760                                                LCK_CR | LCK_CW |
1761                                                LCK_PR | LCK_PW);
1762                         if (mode) {
1763                                 if (lli->lli_flags & (LLIF_DONE_WRITING |
1764                                                       LLIF_EPOCH_PENDING |
1765                                                       LLIF_SOM_DIRTY)) {
1766                                         CERROR("ino %lu flags %u still has "
1767                                                "size authority! do not trust "
1768                                                "the size got from MDS\n",
1769                                                inode->i_ino, lli->lli_flags);
1770                                 } else {
1771                                         /* Use old size assignment to avoid
1772                                          * deadlock bz14138 & bz14326 */
1773                                         i_size_write(inode, body->size);
1774                                         spin_lock(&lli->lli_lock);
1775                                         lli->lli_flags |= LLIF_MDS_SIZE_LOCK;
1776                                         spin_unlock(&lli->lli_lock);
1777                                 }
1778                                 ldlm_lock_decref(&lockh, mode);
1779                         }
1780                 } else {
1781                         /* Use old size assignment to avoid
1782                          * deadlock bz14138 & bz14326 */
1783                         i_size_write(inode, body->size);
1784
1785                         CDEBUG(D_VFSTRACE, "inode=%lu, updating i_size %llu\n",
1786                                inode->i_ino, (unsigned long long)body->size);
1787                 }
1788
1789                 if (body->valid & OBD_MD_FLBLOCKS)
1790                         inode->i_blocks = body->blocks;
1791         }
1792
1793         if (body->valid & OBD_MD_FLMDSCAPA) {
1794                 LASSERT(md->mds_capa);
1795                 ll_add_capa(inode, md->mds_capa);
1796         }
1797         if (body->valid & OBD_MD_FLOSSCAPA) {
1798                 LASSERT(md->oss_capa);
1799                 ll_add_capa(inode, md->oss_capa);
1800         }
1801
1802         if (body->valid & OBD_MD_TSTATE) {
1803                 if (body->t_state & MS_RESTORE)
1804                         lli->lli_flags |= LLIF_FILE_RESTORING;
1805         }
1806 }
1807
1808 void ll_read_inode2(struct inode *inode, void *opaque)
1809 {
1810         struct lustre_md *md = opaque;
1811         struct ll_inode_info *lli = ll_i2info(inode);
1812
1813         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
1814                PFID(&lli->lli_fid), inode);
1815
1816         LASSERT(!lli->lli_has_smd);
1817
1818         /* Core attributes from the MDS first.  This is a new inode, and
1819          * the VFS doesn't zero times in the core inode so we have to do
1820          * it ourselves.  They will be overwritten by either MDS or OST
1821          * attributes - we just need to make sure they aren't newer. */
1822         LTIME_S(inode->i_mtime) = 0;
1823         LTIME_S(inode->i_atime) = 0;
1824         LTIME_S(inode->i_ctime) = 0;
1825         inode->i_rdev = 0;
1826         ll_update_inode(inode, md);
1827
1828         /* OIDEBUG(inode); */
1829
1830         /* initializing backing dev info. */
1831         inode->i_mapping->backing_dev_info = &s2lsi(inode->i_sb)->lsi_bdi;
1832
1833
1834         if (S_ISREG(inode->i_mode)) {
1835                 struct ll_sb_info *sbi = ll_i2sbi(inode);
1836
1837                 inode->i_op = &ll_file_inode_operations;
1838                 inode->i_fop = sbi->ll_fop;
1839                 inode->i_mapping->a_ops = (struct address_space_operations *)&ll_aops;
1840         } else if (S_ISDIR(inode->i_mode)) {
1841                 inode->i_op = &ll_dir_inode_operations;
1842                 inode->i_fop = &ll_dir_operations;
1843         } else if (S_ISLNK(inode->i_mode)) {
1844                 inode->i_op = &ll_fast_symlink_inode_operations;
1845         } else {
1846                 inode->i_op = &ll_special_inode_operations;
1847
1848                 init_special_inode(inode, inode->i_mode,
1849                                    inode->i_rdev);
1850         }
1851 }
1852
1853 void ll_delete_inode(struct inode *inode)
1854 {
1855         struct cl_inode_info *lli = cl_i2info(inode);
1856
1857         if (S_ISREG(inode->i_mode) && lli->lli_clob != NULL)
1858                 /* discard all dirty pages before truncating them, required by
1859                  * osc_extent implementation at LU-1030. */
1860                 cl_sync_file_range(inode, 0, OBD_OBJECT_EOF,
1861                                    CL_FSYNC_DISCARD, 1);
1862
1863         truncate_inode_pages_final(&inode->i_data);
1864
1865         /* Workaround for LU-118 */
1866         if (inode->i_data.nrpages) {
1867                 spin_lock_irq(&inode->i_data.tree_lock);
1868                 spin_unlock_irq(&inode->i_data.tree_lock);
1869                 LASSERTF(inode->i_data.nrpages == 0,
1870                          "inode=%lu/%u(%p) nrpages=%lu, see "
1871                          "http://jira.whamcloud.com/browse/LU-118\n",
1872                          inode->i_ino, inode->i_generation, inode,
1873                          inode->i_data.nrpages);
1874         }
1875         /* Workaround end */
1876
1877         ll_clear_inode(inode);
1878         clear_inode(inode);
1879 }
1880
1881 int ll_iocontrol(struct inode *inode, struct file *file,
1882                  unsigned int cmd, unsigned long arg)
1883 {
1884         struct ll_sb_info *sbi = ll_i2sbi(inode);
1885         struct ptlrpc_request *req = NULL;
1886         int rc, flags = 0;
1887
1888         switch (cmd) {
1889         case FSFILT_IOC_GETFLAGS: {
1890                 struct mdt_body *body;
1891                 struct md_op_data *op_data;
1892
1893                 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL,
1894                                              0, 0, LUSTRE_OPC_ANY,
1895                                              NULL);
1896                 if (IS_ERR(op_data))
1897                         return PTR_ERR(op_data);
1898
1899                 op_data->op_valid = OBD_MD_FLFLAGS;
1900                 rc = md_getattr(sbi->ll_md_exp, op_data, &req);
1901                 ll_finish_md_op_data(op_data);
1902                 if (rc) {
1903                         CERROR("failure %d inode %lu\n", rc, inode->i_ino);
1904                         return -abs(rc);
1905                 }
1906
1907                 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
1908
1909                 flags = body->flags;
1910
1911                 ptlrpc_req_finished(req);
1912
1913                 return put_user(flags, (int *)arg);
1914         }
1915         case FSFILT_IOC_SETFLAGS: {
1916                 struct lov_stripe_md *lsm;
1917                 struct obd_info oinfo = { { { 0 } } };
1918                 struct md_op_data *op_data;
1919
1920                 if (get_user(flags, (int *)arg))
1921                         return -EFAULT;
1922
1923                 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
1924                                              LUSTRE_OPC_ANY, NULL);
1925                 if (IS_ERR(op_data))
1926                         return PTR_ERR(op_data);
1927
1928                 ((struct ll_iattr *)&op_data->op_attr)->ia_attr_flags = flags;
1929                 op_data->op_attr.ia_valid |= ATTR_ATTR_FLAG;
1930                 rc = md_setattr(sbi->ll_md_exp, op_data,
1931                                 NULL, 0, NULL, 0, &req, NULL);
1932                 ll_finish_md_op_data(op_data);
1933                 ptlrpc_req_finished(req);
1934                 if (rc)
1935                         return rc;
1936
1937                 inode->i_flags = ll_ext_to_inode_flags(flags);
1938
1939                 lsm = ccc_inode_lsm_get(inode);
1940                 if (!lsm_has_objects(lsm)) {
1941                         ccc_inode_lsm_put(inode, lsm);
1942                         return 0;
1943                 }
1944
1945                 OBDO_ALLOC(oinfo.oi_oa);
1946                 if (!oinfo.oi_oa) {
1947                         ccc_inode_lsm_put(inode, lsm);
1948                         return -ENOMEM;
1949                 }
1950                 oinfo.oi_md = lsm;
1951                 oinfo.oi_oa->o_oi = lsm->lsm_oi;
1952                 oinfo.oi_oa->o_flags = flags;
1953                 oinfo.oi_oa->o_valid = OBD_MD_FLID | OBD_MD_FLFLAGS |
1954                                        OBD_MD_FLGROUP;
1955                 oinfo.oi_capa = ll_mdscapa_get(inode);
1956                 obdo_set_parent_fid(oinfo.oi_oa, &ll_i2info(inode)->lli_fid);
1957                 rc = obd_setattr_rqset(sbi->ll_dt_exp, &oinfo, NULL);
1958                 capa_put(oinfo.oi_capa);
1959                 OBDO_FREE(oinfo.oi_oa);
1960                 ccc_inode_lsm_put(inode, lsm);
1961
1962                 if (rc && rc != -EPERM && rc != -EACCES)
1963                         CERROR("osc_setattr_async fails: rc = %d\n", rc);
1964
1965                 return rc;
1966         }
1967         default:
1968                 return -ENOSYS;
1969         }
1970
1971         return 0;
1972 }
1973
1974 int ll_flush_ctx(struct inode *inode)
1975 {
1976         struct ll_sb_info  *sbi = ll_i2sbi(inode);
1977
1978         CDEBUG(D_SEC, "flush context for user %d\n",
1979                       from_kuid(&init_user_ns, current_uid()));
1980
1981         obd_set_info_async(NULL, sbi->ll_md_exp,
1982                            sizeof(KEY_FLUSH_CTX), KEY_FLUSH_CTX,
1983                            0, NULL, NULL);
1984         obd_set_info_async(NULL, sbi->ll_dt_exp,
1985                            sizeof(KEY_FLUSH_CTX), KEY_FLUSH_CTX,
1986                            0, NULL, NULL);
1987         return 0;
1988 }
1989
1990 /* umount -f client means force down, don't save state */
1991 void ll_umount_begin(struct super_block *sb)
1992 {
1993         struct ll_sb_info *sbi = ll_s2sbi(sb);
1994         struct obd_device *obd;
1995         struct obd_ioctl_data *ioc_data;
1996
1997         CDEBUG(D_VFSTRACE, "VFS Op: superblock %p count %d active %d\n", sb,
1998                sb->s_count, atomic_read(&sb->s_active));
1999
2000         obd = class_exp2obd(sbi->ll_md_exp);
2001         if (obd == NULL) {
2002                 CERROR("Invalid MDC connection handle %#llx\n",
2003                        sbi->ll_md_exp->exp_handle.h_cookie);
2004                 return;
2005         }
2006         obd->obd_force = 1;
2007
2008         obd = class_exp2obd(sbi->ll_dt_exp);
2009         if (obd == NULL) {
2010                 CERROR("Invalid LOV connection handle %#llx\n",
2011                        sbi->ll_dt_exp->exp_handle.h_cookie);
2012                 return;
2013         }
2014         obd->obd_force = 1;
2015
2016         ioc_data = kzalloc(sizeof(*ioc_data), GFP_NOFS);
2017         if (ioc_data) {
2018                 obd_iocontrol(IOC_OSC_SET_ACTIVE, sbi->ll_md_exp,
2019                               sizeof(*ioc_data), ioc_data, NULL);
2020
2021                 obd_iocontrol(IOC_OSC_SET_ACTIVE, sbi->ll_dt_exp,
2022                               sizeof(*ioc_data), ioc_data, NULL);
2023
2024                 OBD_FREE_PTR(ioc_data);
2025         }
2026
2027         /* Really, we'd like to wait until there are no requests outstanding,
2028          * and then continue.  For now, we just invalidate the requests,
2029          * schedule() and sleep one second if needed, and hope.
2030          */
2031         schedule();
2032 }
2033
2034 int ll_remount_fs(struct super_block *sb, int *flags, char *data)
2035 {
2036         struct ll_sb_info *sbi = ll_s2sbi(sb);
2037         char *profilenm = get_profile_name(sb);
2038         int err;
2039         __u32 read_only;
2040
2041         if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY)) {
2042                 read_only = *flags & MS_RDONLY;
2043                 err = obd_set_info_async(NULL, sbi->ll_md_exp,
2044                                          sizeof(KEY_READ_ONLY),
2045                                          KEY_READ_ONLY, sizeof(read_only),
2046                                          &read_only, NULL);
2047                 if (err) {
2048                         LCONSOLE_WARN("Failed to remount %s %s (%d)\n",
2049                                       profilenm, read_only ?
2050                                       "read-only" : "read-write", err);
2051                         return err;
2052                 }
2053
2054                 if (read_only)
2055                         sb->s_flags |= MS_RDONLY;
2056                 else
2057                         sb->s_flags &= ~MS_RDONLY;
2058
2059                 if (sbi->ll_flags & LL_SBI_VERBOSE)
2060                         LCONSOLE_WARN("Remounted %s %s\n", profilenm,
2061                                       read_only ?  "read-only" : "read-write");
2062         }
2063         return 0;
2064 }
2065
2066 int ll_prep_inode(struct inode **inode, struct ptlrpc_request *req,
2067                   struct super_block *sb, struct lookup_intent *it)
2068 {
2069         struct ll_sb_info *sbi = NULL;
2070         struct lustre_md md;
2071         int rc;
2072
2073         LASSERT(*inode || sb);
2074         sbi = sb ? ll_s2sbi(sb) : ll_i2sbi(*inode);
2075         rc = md_get_lustre_md(sbi->ll_md_exp, req, sbi->ll_dt_exp,
2076                               sbi->ll_md_exp, &md);
2077         if (rc)
2078                 return rc;
2079
2080         if (*inode) {
2081                 ll_update_inode(*inode, &md);
2082         } else {
2083                 LASSERT(sb != NULL);
2084
2085                 /*
2086                  * At this point server returns to client's same fid as client
2087                  * generated for creating. So using ->fid1 is okay here.
2088                  */
2089                 LASSERT(fid_is_sane(&md.body->fid1));
2090
2091                 *inode = ll_iget(sb, cl_fid_build_ino(&md.body->fid1,
2092                                              sbi->ll_flags & LL_SBI_32BIT_API),
2093                                  &md);
2094                 if (*inode == NULL || IS_ERR(*inode)) {
2095 #ifdef CONFIG_FS_POSIX_ACL
2096                         if (md.posix_acl) {
2097                                 posix_acl_release(md.posix_acl);
2098                                 md.posix_acl = NULL;
2099                         }
2100 #endif
2101                         rc = IS_ERR(*inode) ? PTR_ERR(*inode) : -ENOMEM;
2102                         *inode = NULL;
2103                         CERROR("new_inode -fatal: rc %d\n", rc);
2104                         goto out;
2105                 }
2106         }
2107
2108         /* Handling piggyback layout lock.
2109          * Layout lock can be piggybacked by getattr and open request.
2110          * The lsm can be applied to inode only if it comes with a layout lock
2111          * otherwise correct layout may be overwritten, for example:
2112          * 1. proc1: mdt returns a lsm but not granting layout
2113          * 2. layout was changed by another client
2114          * 3. proc2: refresh layout and layout lock granted
2115          * 4. proc1: to apply a stale layout */
2116         if (it != NULL && it->d.lustre.it_lock_mode != 0) {
2117                 struct lustre_handle lockh;
2118                 struct ldlm_lock *lock;
2119
2120                 lockh.cookie = it->d.lustre.it_lock_handle;
2121                 lock = ldlm_handle2lock(&lockh);
2122                 LASSERT(lock != NULL);
2123                 if (ldlm_has_layout(lock)) {
2124                         struct cl_object_conf conf;
2125
2126                         memset(&conf, 0, sizeof(conf));
2127                         conf.coc_opc = OBJECT_CONF_SET;
2128                         conf.coc_inode = *inode;
2129                         conf.coc_lock = lock;
2130                         conf.u.coc_md = &md;
2131                         (void)ll_layout_conf(*inode, &conf);
2132                 }
2133                 LDLM_LOCK_PUT(lock);
2134         }
2135
2136 out:
2137         if (md.lsm != NULL)
2138                 obd_free_memmd(sbi->ll_dt_exp, &md.lsm);
2139         md_free_lustre_md(sbi->ll_md_exp, &md);
2140         return rc;
2141 }
2142
2143 int ll_obd_statfs(struct inode *inode, void *arg)
2144 {
2145         struct ll_sb_info *sbi = NULL;
2146         struct obd_export *exp;
2147         char *buf = NULL;
2148         struct obd_ioctl_data *data = NULL;
2149         __u32 type;
2150         __u32 flags;
2151         int len = 0, rc;
2152
2153         if (!inode) {
2154                 rc = -EINVAL;
2155                 goto out_statfs;
2156         }
2157
2158         sbi = ll_i2sbi(inode);
2159         if (!sbi) {
2160                 rc = -EINVAL;
2161                 goto out_statfs;
2162         }
2163
2164         rc = obd_ioctl_getdata(&buf, &len, arg);
2165         if (rc)
2166                 goto out_statfs;
2167
2168         data = (void *)buf;
2169         if (!data->ioc_inlbuf1 || !data->ioc_inlbuf2 ||
2170             !data->ioc_pbuf1 || !data->ioc_pbuf2) {
2171                 rc = -EINVAL;
2172                 goto out_statfs;
2173         }
2174
2175         if (data->ioc_inllen1 != sizeof(__u32) ||
2176             data->ioc_inllen2 != sizeof(__u32) ||
2177             data->ioc_plen1 != sizeof(struct obd_statfs) ||
2178             data->ioc_plen2 != sizeof(struct obd_uuid)) {
2179                 rc = -EINVAL;
2180                 goto out_statfs;
2181         }
2182
2183         memcpy(&type, data->ioc_inlbuf1, sizeof(__u32));
2184         if (type & LL_STATFS_LMV)
2185                 exp = sbi->ll_md_exp;
2186         else if (type & LL_STATFS_LOV)
2187                 exp = sbi->ll_dt_exp;
2188         else {
2189                 rc = -ENODEV;
2190                 goto out_statfs;
2191         }
2192
2193         flags = (type & LL_STATFS_NODELAY) ? OBD_STATFS_NODELAY : 0;
2194         rc = obd_iocontrol(IOC_OBD_STATFS, exp, len, buf, &flags);
2195         if (rc)
2196                 goto out_statfs;
2197 out_statfs:
2198         if (buf)
2199                 obd_ioctl_freedata(buf, len);
2200         return rc;
2201 }
2202
2203 int ll_process_config(struct lustre_cfg *lcfg)
2204 {
2205         char *ptr;
2206         void *sb;
2207         struct lprocfs_static_vars lvars;
2208         unsigned long x;
2209         int rc = 0;
2210
2211         lprocfs_llite_init_vars(&lvars);
2212
2213         /* The instance name contains the sb: lustre-client-aacfe000 */
2214         ptr = strrchr(lustre_cfg_string(lcfg, 0), '-');
2215         if (!ptr || !*(++ptr))
2216                 return -EINVAL;
2217         if (sscanf(ptr, "%lx", &x) != 1)
2218                 return -EINVAL;
2219         sb = (void *)x;
2220         /* This better be a real Lustre superblock! */
2221         LASSERT(s2lsi((struct super_block *)sb)->lsi_lmd->lmd_magic == LMD_MAGIC);
2222
2223         /* Note we have not called client_common_fill_super yet, so
2224            proc fns must be able to handle that! */
2225         rc = class_process_proc_param(PARAM_LLITE, lvars.obd_vars,
2226                                       lcfg, sb);
2227         if (rc > 0)
2228                 rc = 0;
2229         return rc;
2230 }
2231
2232 /* this function prepares md_op_data hint for passing ot down to MD stack. */
2233 struct md_op_data *ll_prep_md_op_data(struct md_op_data *op_data,
2234                                        struct inode *i1, struct inode *i2,
2235                                        const char *name, int namelen,
2236                                        int mode, __u32 opc, void *data)
2237 {
2238         LASSERT(i1 != NULL);
2239
2240         if (namelen > ll_i2sbi(i1)->ll_namelen)
2241                 return ERR_PTR(-ENAMETOOLONG);
2242
2243         if (op_data == NULL)
2244                 op_data = kzalloc(sizeof(*op_data), GFP_NOFS);
2245
2246         if (op_data == NULL)
2247                 return ERR_PTR(-ENOMEM);
2248
2249         ll_i2gids(op_data->op_suppgids, i1, i2);
2250         op_data->op_fid1 = *ll_inode2fid(i1);
2251         op_data->op_capa1 = ll_mdscapa_get(i1);
2252
2253         if (i2) {
2254                 op_data->op_fid2 = *ll_inode2fid(i2);
2255                 op_data->op_capa2 = ll_mdscapa_get(i2);
2256         } else {
2257                 fid_zero(&op_data->op_fid2);
2258                 op_data->op_capa2 = NULL;
2259         }
2260
2261         op_data->op_name = name;
2262         op_data->op_namelen = namelen;
2263         op_data->op_mode = mode;
2264         op_data->op_mod_time = get_seconds();
2265         op_data->op_fsuid = from_kuid(&init_user_ns, current_fsuid());
2266         op_data->op_fsgid = from_kgid(&init_user_ns, current_fsgid());
2267         op_data->op_cap = cfs_curproc_cap_pack();
2268         op_data->op_bias = 0;
2269         op_data->op_cli_flags = 0;
2270         if ((opc == LUSTRE_OPC_CREATE) && (name != NULL) &&
2271              filename_is_volatile(name, namelen, NULL))
2272                 op_data->op_bias |= MDS_CREATE_VOLATILE;
2273         op_data->op_opc = opc;
2274         op_data->op_mds = 0;
2275         op_data->op_data = data;
2276
2277         /* If the file is being opened after mknod() (normally due to NFS)
2278          * try to use the default stripe data from parent directory for
2279          * allocating OST objects.  Try to pass the parent FID to MDS. */
2280         if (opc == LUSTRE_OPC_CREATE && i1 == i2 && S_ISREG(i2->i_mode) &&
2281             !ll_i2info(i2)->lli_has_smd) {
2282                 struct ll_inode_info *lli = ll_i2info(i2);
2283
2284                 spin_lock(&lli->lli_lock);
2285                 if (likely(!lli->lli_has_smd && !fid_is_zero(&lli->lli_pfid)))
2286                         op_data->op_fid1 = lli->lli_pfid;
2287                 spin_unlock(&lli->lli_lock);
2288                 /** We ignore parent's capability temporary. */
2289         }
2290
2291         /* When called by ll_setattr_raw, file is i1. */
2292         if (LLIF_DATA_MODIFIED & ll_i2info(i1)->lli_flags)
2293                 op_data->op_bias |= MDS_DATA_MODIFIED;
2294
2295         return op_data;
2296 }
2297
2298 void ll_finish_md_op_data(struct md_op_data *op_data)
2299 {
2300         capa_put(op_data->op_capa1);
2301         capa_put(op_data->op_capa2);
2302         OBD_FREE_PTR(op_data);
2303 }
2304
2305 int ll_show_options(struct seq_file *seq, struct dentry *dentry)
2306 {
2307         struct ll_sb_info *sbi;
2308
2309         LASSERT((seq != NULL) && (dentry != NULL));
2310         sbi = ll_s2sbi(dentry->d_sb);
2311
2312         if (sbi->ll_flags & LL_SBI_NOLCK)
2313                 seq_puts(seq, ",nolock");
2314
2315         if (sbi->ll_flags & LL_SBI_FLOCK)
2316                 seq_puts(seq, ",flock");
2317
2318         if (sbi->ll_flags & LL_SBI_LOCALFLOCK)
2319                 seq_puts(seq, ",localflock");
2320
2321         if (sbi->ll_flags & LL_SBI_USER_XATTR)
2322                 seq_puts(seq, ",user_xattr");
2323
2324         if (sbi->ll_flags & LL_SBI_LAZYSTATFS)
2325                 seq_puts(seq, ",lazystatfs");
2326
2327         if (sbi->ll_flags & LL_SBI_USER_FID2PATH)
2328                 seq_puts(seq, ",user_fid2path");
2329
2330         return 0;
2331 }
2332
2333 /**
2334  * Get obd name by cmd, and copy out to user space
2335  */
2336 int ll_get_obd_name(struct inode *inode, unsigned int cmd, unsigned long arg)
2337 {
2338         struct ll_sb_info *sbi = ll_i2sbi(inode);
2339         struct obd_device *obd;
2340
2341         if (cmd == OBD_IOC_GETDTNAME)
2342                 obd = class_exp2obd(sbi->ll_dt_exp);
2343         else if (cmd == OBD_IOC_GETMDNAME)
2344                 obd = class_exp2obd(sbi->ll_md_exp);
2345         else
2346                 return -EINVAL;
2347
2348         if (!obd)
2349                 return -ENOENT;
2350
2351         if (copy_to_user((void *)arg, obd->obd_name,
2352                              strlen(obd->obd_name) + 1))
2353                 return -EFAULT;
2354
2355         return 0;
2356 }
2357
2358 /**
2359  * Get lustre file system name by \a sbi. If \a buf is provided(non-NULL), the
2360  * fsname will be returned in this buffer; otherwise, a static buffer will be
2361  * used to store the fsname and returned to caller.
2362  */
2363 char *ll_get_fsname(struct super_block *sb, char *buf, int buflen)
2364 {
2365         static char fsname_static[MTI_NAME_MAXLEN];
2366         struct lustre_sb_info *lsi = s2lsi(sb);
2367         char *ptr;
2368         int len;
2369
2370         if (buf == NULL) {
2371                 /* this means the caller wants to use static buffer
2372                  * and it doesn't care about race. Usually this is
2373                  * in error reporting path */
2374                 buf = fsname_static;
2375                 buflen = sizeof(fsname_static);
2376         }
2377
2378         len = strlen(lsi->lsi_lmd->lmd_profile);
2379         ptr = strrchr(lsi->lsi_lmd->lmd_profile, '-');
2380         if (ptr && (strcmp(ptr, "-client") == 0))
2381                 len -= 7;
2382
2383         if (unlikely(len >= buflen))
2384                 len = buflen - 1;
2385         strncpy(buf, lsi->lsi_lmd->lmd_profile, len);
2386         buf[len] = '\0';
2387
2388         return buf;
2389 }
2390
2391 static char *ll_d_path(struct dentry *dentry, char *buf, int bufsize)
2392 {
2393         char *path = NULL;
2394
2395         struct path p;
2396
2397         p.dentry = dentry;
2398         p.mnt = current->fs->root.mnt;
2399         path_get(&p);
2400         path = d_path(&p, buf, bufsize);
2401         path_put(&p);
2402
2403         return path;
2404 }
2405
2406 void ll_dirty_page_discard_warn(struct page *page, int ioret)
2407 {
2408         char *buf, *path = NULL;
2409         struct dentry *dentry = NULL;
2410         struct ccc_object *obj = cl_inode2ccc(page->mapping->host);
2411
2412         /* this can be called inside spin lock so use GFP_ATOMIC. */
2413         buf = (char *)__get_free_page(GFP_ATOMIC);
2414         if (buf != NULL) {
2415                 dentry = d_find_alias(page->mapping->host);
2416                 if (dentry != NULL)
2417                         path = ll_d_path(dentry, buf, PAGE_SIZE);
2418         }
2419
2420         CDEBUG(D_WARNING,
2421                "%s: dirty page discard: %s/fid: "DFID"/%s may get corrupted "
2422                "(rc %d)\n", ll_get_fsname(page->mapping->host->i_sb, NULL, 0),
2423                s2lsi(page->mapping->host->i_sb)->lsi_lmd->lmd_dev,
2424                PFID(&obj->cob_header.coh_lu.loh_fid),
2425                (path && !IS_ERR(path)) ? path : "", ioret);
2426
2427         if (dentry != NULL)
2428                 dput(dentry);
2429
2430         if (buf != NULL)
2431                 free_page((unsigned long)buf);
2432 }