Whamcloud - gitweb
7f696c873a92763c1ebfec0171a395d6c0c1ee0d
[fs/lustre-release.git] / 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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/llite/llite_lib.c
33  *
34  * Lustre Light Super operations
35  */
36
37 #define DEBUG_SUBSYSTEM S_LLITE
38
39 #include <linux/module.h>
40 #include <linux/random.h>
41 #include <linux/statfs.h>
42 #include <linux/time.h>
43 #include <linux/types.h>
44 #include <libcfs/linux/linux-uuid.h>
45 #include <linux/version.h>
46 #include <linux/mm.h>
47 #include <linux/user_namespace.h>
48 #ifdef HAVE_UIDGID_HEADER
49 # include <linux/uidgid.h>
50 #endif
51 #include <linux/security.h>
52
53 #include <uapi/linux/lustre/lustre_ioctl.h>
54 #include <lustre_ha.h>
55 #include <lustre_dlm.h>
56 #include <lprocfs_status.h>
57 #include <lustre_disk.h>
58 #include <uapi/linux/lustre/lustre_param.h>
59 #include <lustre_log.h>
60 #include <cl_object.h>
61 #include <obd_cksum.h>
62 #include "llite_internal.h"
63
64 struct kmem_cache *ll_file_data_slab;
65
66 #ifndef log2
67 #define log2(n) ffz(~(n))
68 #endif
69
70 static struct ll_sb_info *ll_init_sbi(void)
71 {
72         struct ll_sb_info *sbi = NULL;
73         unsigned long pages;
74         unsigned long lru_page_max;
75         struct sysinfo si;
76         int i;
77         ENTRY;
78
79         OBD_ALLOC_PTR(sbi);
80         if (sbi == NULL)
81                 RETURN(NULL);
82
83         spin_lock_init(&sbi->ll_lock);
84         mutex_init(&sbi->ll_lco.lco_lock);
85         spin_lock_init(&sbi->ll_pp_extent_lock);
86         spin_lock_init(&sbi->ll_process_lock);
87         sbi->ll_rw_stats_on = 0;
88
89         si_meminfo(&si);
90         pages = si.totalram - si.totalhigh;
91         lru_page_max = pages / 2;
92
93         /* initialize ll_cache data */
94         sbi->ll_cache = cl_cache_init(lru_page_max);
95         if (sbi->ll_cache == NULL) {
96                 OBD_FREE(sbi, sizeof(*sbi));
97                 RETURN(NULL);
98         }
99
100         sbi->ll_ra_info.ra_max_pages_per_file = min(pages / 32,
101                                            SBI_DEFAULT_READAHEAD_MAX);
102         sbi->ll_ra_info.ra_max_pages = sbi->ll_ra_info.ra_max_pages_per_file;
103         sbi->ll_ra_info.ra_max_read_ahead_whole_pages = -1;
104
105         sbi->ll_flags |= LL_SBI_VERBOSE;
106 #ifdef ENABLE_CHECKSUM
107         sbi->ll_flags |= LL_SBI_CHECKSUM;
108 #endif
109 #ifdef ENABLE_FLOCK
110         sbi->ll_flags |= LL_SBI_FLOCK;
111 #endif
112
113 #ifdef HAVE_LRU_RESIZE_SUPPORT
114         sbi->ll_flags |= LL_SBI_LRU_RESIZE;
115 #endif
116         sbi->ll_flags |= LL_SBI_LAZYSTATFS;
117
118         for (i = 0; i <= LL_PROCESS_HIST_MAX; i++) {
119                 spin_lock_init(&sbi->ll_rw_extents_info.pp_extents[i].
120                                pp_r_hist.oh_lock);
121                 spin_lock_init(&sbi->ll_rw_extents_info.pp_extents[i].
122                                pp_w_hist.oh_lock);
123         }
124
125         /* metadata statahead is enabled by default */
126         sbi->ll_sa_running_max = LL_SA_RUNNING_DEF;
127         sbi->ll_sa_max = LL_SA_RPC_DEF;
128         atomic_set(&sbi->ll_sa_total, 0);
129         atomic_set(&sbi->ll_sa_wrong, 0);
130         atomic_set(&sbi->ll_sa_running, 0);
131         atomic_set(&sbi->ll_agl_total, 0);
132         sbi->ll_flags |= LL_SBI_AGL_ENABLED;
133         sbi->ll_flags |= LL_SBI_FAST_READ;
134         sbi->ll_flags |= LL_SBI_TINY_WRITE;
135
136         /* root squash */
137         sbi->ll_squash.rsi_uid = 0;
138         sbi->ll_squash.rsi_gid = 0;
139         INIT_LIST_HEAD(&sbi->ll_squash.rsi_nosquash_nids);
140         init_rwsem(&sbi->ll_squash.rsi_sem);
141         pcc_super_init(&sbi->ll_pcc_super);
142
143         /* Per-filesystem file heat */
144         sbi->ll_heat_decay_weight = SBI_DEFAULT_HEAT_DECAY_WEIGHT;
145         sbi->ll_heat_period_second = SBI_DEFAULT_HEAT_PERIOD_SECOND;
146         RETURN(sbi);
147 }
148
149 static void ll_free_sbi(struct super_block *sb)
150 {
151         struct ll_sb_info *sbi = ll_s2sbi(sb);
152         ENTRY;
153
154         if (sbi != NULL) {
155                 if (!list_empty(&sbi->ll_squash.rsi_nosquash_nids))
156                         cfs_free_nidlist(&sbi->ll_squash.rsi_nosquash_nids);
157                 if (sbi->ll_cache != NULL) {
158                         cl_cache_decref(sbi->ll_cache);
159                         sbi->ll_cache = NULL;
160                 }
161                 pcc_super_fini(&sbi->ll_pcc_super);
162                 OBD_FREE(sbi, sizeof(*sbi));
163         }
164         EXIT;
165 }
166
167 static int client_common_fill_super(struct super_block *sb, char *md, char *dt,
168                                     struct vfsmount *mnt)
169 {
170         struct inode *root = NULL;
171         struct ll_sb_info *sbi = ll_s2sbi(sb);
172         struct obd_statfs *osfs = NULL;
173         struct ptlrpc_request *request = NULL;
174         struct obd_connect_data *data = NULL;
175         struct obd_uuid *uuid;
176         struct md_op_data *op_data;
177         struct lustre_md lmd;
178         u64 valid;
179         int size, err, checksum;
180
181         ENTRY;
182         sbi->ll_md_obd = class_name2obd(md);
183         if (!sbi->ll_md_obd) {
184                 CERROR("MD %s: not setup or attached\n", md);
185                 RETURN(-EINVAL);
186         }
187
188         OBD_ALLOC_PTR(data);
189         if (data == NULL)
190                 RETURN(-ENOMEM);
191
192         OBD_ALLOC_PTR(osfs);
193         if (osfs == NULL) {
194                 OBD_FREE_PTR(data);
195                 RETURN(-ENOMEM);
196         }
197
198         /* pass client page size via ocd_grant_blkbits, the server should report
199          * back its backend blocksize for grant calculation purpose */
200         data->ocd_grant_blkbits = PAGE_SHIFT;
201
202         /* indicate MDT features supported by this client */
203         data->ocd_connect_flags = OBD_CONNECT_IBITS    | OBD_CONNECT_NODEVOH  |
204                                   OBD_CONNECT_ATTRFID  | OBD_CONNECT_GRANT |
205                                   OBD_CONNECT_VERSION  | OBD_CONNECT_BRW_SIZE |
206                                   OBD_CONNECT_SRVLOCK  | OBD_CONNECT_TRUNCLOCK|
207                                   OBD_CONNECT_MDS_CAPA | OBD_CONNECT_OSS_CAPA |
208                                   OBD_CONNECT_CANCELSET | OBD_CONNECT_FID     |
209                                   OBD_CONNECT_AT       | OBD_CONNECT_LOV_V3   |
210                                   OBD_CONNECT_VBR | OBD_CONNECT_FULL20 |
211                                   OBD_CONNECT_64BITHASH |
212                                   OBD_CONNECT_EINPROGRESS |
213                                   OBD_CONNECT_JOBSTATS | OBD_CONNECT_LVB_TYPE |
214                                   OBD_CONNECT_LAYOUTLOCK | OBD_CONNECT_PINGLESS|
215                                   OBD_CONNECT_MAX_EASIZE |
216                                   OBD_CONNECT_FLOCK_DEAD |
217                                   OBD_CONNECT_DISP_STRIPE | OBD_CONNECT_LFSCK |
218                                   OBD_CONNECT_OPEN_BY_FID |
219                                   OBD_CONNECT_DIR_STRIPE |
220                                   OBD_CONNECT_BULK_MBITS | OBD_CONNECT_CKSUM |
221                                   OBD_CONNECT_SUBTREE |
222                                   OBD_CONNECT_MULTIMODRPCS |
223                                   OBD_CONNECT_GRANT_PARAM |
224                                   OBD_CONNECT_SHORTIO | OBD_CONNECT_FLAGS2;
225
226         data->ocd_connect_flags2 = OBD_CONNECT2_DIR_MIGRATE |
227                                    OBD_CONNECT2_SUM_STATFS |
228                                    OBD_CONNECT2_OVERSTRIPING |
229                                    OBD_CONNECT2_FLR |
230                                    OBD_CONNECT2_LOCK_CONVERT |
231                                    OBD_CONNECT2_ARCHIVE_ID_ARRAY |
232                                    OBD_CONNECT2_LSOM |
233                                    OBD_CONNECT2_ASYNC_DISCARD |
234                                    OBD_CONNECT2_PCC;
235
236 #ifdef HAVE_LRU_RESIZE_SUPPORT
237         if (sbi->ll_flags & LL_SBI_LRU_RESIZE)
238                 data->ocd_connect_flags |= OBD_CONNECT_LRU_RESIZE;
239 #endif
240 #ifdef CONFIG_FS_POSIX_ACL
241         data->ocd_connect_flags |= OBD_CONNECT_ACL | OBD_CONNECT_UMASK |
242                                    OBD_CONNECT_LARGE_ACL;
243 #endif
244
245         data->ocd_cksum_types = obd_cksum_types_supported_client();
246
247         if (OBD_FAIL_CHECK(OBD_FAIL_MDC_LIGHTWEIGHT))
248                 /* flag mdc connection as lightweight, only used for test
249                  * purpose, use with care */
250                 data->ocd_connect_flags |= OBD_CONNECT_LIGHTWEIGHT;
251
252         data->ocd_ibits_known = MDS_INODELOCK_FULL;
253         data->ocd_version = LUSTRE_VERSION_CODE;
254
255         if (sb->s_flags & MS_RDONLY)
256                 data->ocd_connect_flags |= OBD_CONNECT_RDONLY;
257         if (sbi->ll_flags & LL_SBI_USER_XATTR)
258                 data->ocd_connect_flags |= OBD_CONNECT_XATTR;
259
260 #ifdef MS_NOSEC
261         /* Setting this indicates we correctly support S_NOSEC (See kernel
262          * commit 9e1f1de02c2275d7172e18dc4e7c2065777611bf)
263          */
264         sb->s_flags |= MS_NOSEC;
265 #endif
266
267         if (sbi->ll_flags & LL_SBI_FLOCK)
268                 sbi->ll_fop = &ll_file_operations_flock;
269         else if (sbi->ll_flags & LL_SBI_LOCALFLOCK)
270                 sbi->ll_fop = &ll_file_operations;
271         else
272                 sbi->ll_fop = &ll_file_operations_noflock;
273
274         /* always ping even if server suppress_pings */
275         if (sbi->ll_flags & LL_SBI_ALWAYS_PING)
276                 data->ocd_connect_flags &= ~OBD_CONNECT_PINGLESS;
277
278         obd_connect_set_secctx(data);
279
280 #if defined(CONFIG_SECURITY)
281         data->ocd_connect_flags2 |= OBD_CONNECT2_SELINUX_POLICY;
282 #endif
283
284         data->ocd_brw_size = MD_MAX_BRW_SIZE;
285
286         err = obd_connect(NULL, &sbi->ll_md_exp, sbi->ll_md_obd,
287                           &sbi->ll_sb_uuid, data, sbi->ll_cache);
288         if (err == -EBUSY) {
289                 LCONSOLE_ERROR_MSG(0x14f, "An MDT (md %s) is performing "
290                                    "recovery, of which this client is not a "
291                                    "part. Please wait for recovery to complete,"
292                                    " abort, or time out.\n", md);
293                 GOTO(out, err);
294         } else if (err) {
295                 CERROR("cannot connect to %s: rc = %d\n", md, err);
296                 GOTO(out, err);
297         }
298
299         sbi->ll_md_exp->exp_connect_data = *data;
300
301         err = obd_fid_init(sbi->ll_md_exp->exp_obd, sbi->ll_md_exp,
302                            LUSTRE_SEQ_METADATA);
303         if (err) {
304                 CERROR("%s: Can't init metadata layer FID infrastructure, "
305                        "rc = %d\n", sbi->ll_md_exp->exp_obd->obd_name, err);
306                 GOTO(out_md, err);
307         }
308
309         /* For mount, we only need fs info from MDT0, and also in DNE, it
310          * can make sure the client can be mounted as long as MDT0 is
311          * avaible */
312         err = obd_statfs(NULL, sbi->ll_md_exp, osfs,
313                         ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS,
314                         OBD_STATFS_FOR_MDT0);
315         if (err)
316                 GOTO(out_md_fid, err);
317
318         /* This needs to be after statfs to ensure connect has finished.
319          * Note that "data" does NOT contain the valid connect reply.
320          * If connecting to a 1.8 server there will be no LMV device, so
321          * we can access the MDC export directly and exp_connect_flags will
322          * be non-zero, but if accessing an upgraded 2.1 server it will
323          * have the correct flags filled in.
324          * XXX: fill in the LMV exp_connect_flags from MDC(s). */
325         valid = exp_connect_flags(sbi->ll_md_exp) & CLIENT_CONNECT_MDT_REQD;
326         if (exp_connect_flags(sbi->ll_md_exp) != 0 &&
327             valid != CLIENT_CONNECT_MDT_REQD) {
328                 char *buf;
329
330                 OBD_ALLOC_WAIT(buf, PAGE_SIZE);
331                 obd_connect_flags2str(buf, PAGE_SIZE,
332                                       valid ^ CLIENT_CONNECT_MDT_REQD, 0, ",");
333                 LCONSOLE_ERROR_MSG(0x170, "Server %s does not support "
334                                    "feature(s) needed for correct operation "
335                                    "of this client (%s). Please upgrade "
336                                    "server or downgrade client.\n",
337                                    sbi->ll_md_exp->exp_obd->obd_name, buf);
338                 OBD_FREE(buf, PAGE_SIZE);
339                 GOTO(out_md_fid, err = -EPROTO);
340         }
341
342         size = sizeof(*data);
343         err = obd_get_info(NULL, sbi->ll_md_exp, sizeof(KEY_CONN_DATA),
344                            KEY_CONN_DATA,  &size, data);
345         if (err) {
346                 CERROR("%s: Get connect data failed: rc = %d\n",
347                        sbi->ll_md_exp->exp_obd->obd_name, err);
348                 GOTO(out_md_fid, err);
349         }
350
351         LASSERT(osfs->os_bsize);
352         sb->s_blocksize = osfs->os_bsize;
353         sb->s_blocksize_bits = log2(osfs->os_bsize);
354         sb->s_magic = LL_SUPER_MAGIC;
355         sb->s_maxbytes = MAX_LFS_FILESIZE;
356         sbi->ll_namelen = osfs->os_namelen;
357         sbi->ll_mnt.mnt = current->fs->root.mnt;
358
359         if ((sbi->ll_flags & LL_SBI_USER_XATTR) &&
360             !(data->ocd_connect_flags & OBD_CONNECT_XATTR)) {
361                 LCONSOLE_INFO("Disabling user_xattr feature because "
362                               "it is not supported on the server\n");
363                 sbi->ll_flags &= ~LL_SBI_USER_XATTR;
364         }
365
366         if (data->ocd_connect_flags & OBD_CONNECT_ACL) {
367 #ifdef MS_POSIXACL
368                 sb->s_flags |= MS_POSIXACL;
369 #endif
370                 sbi->ll_flags |= LL_SBI_ACL;
371         } else {
372                 LCONSOLE_INFO("client wants to enable acl, but mdt not!\n");
373 #ifdef MS_POSIXACL
374                 sb->s_flags &= ~MS_POSIXACL;
375 #endif
376                 sbi->ll_flags &= ~LL_SBI_ACL;
377         }
378
379         if (data->ocd_connect_flags & OBD_CONNECT_64BITHASH)
380                 sbi->ll_flags |= LL_SBI_64BIT_HASH;
381
382         if (data->ocd_connect_flags & OBD_CONNECT_LAYOUTLOCK)
383                 sbi->ll_flags |= LL_SBI_LAYOUT_LOCK;
384
385         if (obd_connect_has_secctx(data))
386                 sbi->ll_flags |= LL_SBI_FILE_SECCTX;
387
388         if (data->ocd_ibits_known & MDS_INODELOCK_XATTR) {
389                 if (!(data->ocd_connect_flags & OBD_CONNECT_MAX_EASIZE)) {
390                         LCONSOLE_INFO("%s: disabling xattr cache due to "
391                                       "unknown maximum xattr size.\n", dt);
392                 } else if (!sbi->ll_xattr_cache_set) {
393                         /* If xattr_cache is already set (no matter 0 or 1)
394                          * during processing llog, it won't be enabled here. */
395                         sbi->ll_flags |= LL_SBI_XATTR_CACHE;
396                         sbi->ll_xattr_cache_enabled = 1;
397                 }
398         }
399
400         sbi->ll_dt_obd = class_name2obd(dt);
401         if (!sbi->ll_dt_obd) {
402                 CERROR("DT %s: not setup or attached\n", dt);
403                 GOTO(out_md_fid, err = -ENODEV);
404         }
405
406         /* pass client page size via ocd_grant_blkbits, the server should report
407          * back its backend blocksize for grant calculation purpose */
408         data->ocd_grant_blkbits = PAGE_SHIFT;
409
410         /* indicate OST features supported by this client */
411         data->ocd_connect_flags = OBD_CONNECT_GRANT | OBD_CONNECT_VERSION |
412                                   OBD_CONNECT_REQPORTAL | OBD_CONNECT_BRW_SIZE |
413                                   OBD_CONNECT_CANCELSET | OBD_CONNECT_FID |
414                                   OBD_CONNECT_SRVLOCK | OBD_CONNECT_TRUNCLOCK|
415                                   OBD_CONNECT_AT | OBD_CONNECT_OSS_CAPA |
416                                   OBD_CONNECT_VBR | OBD_CONNECT_FULL20 |
417                                   OBD_CONNECT_64BITHASH | OBD_CONNECT_MAXBYTES |
418                                   OBD_CONNECT_EINPROGRESS |
419                                   OBD_CONNECT_JOBSTATS | OBD_CONNECT_LVB_TYPE |
420                                   OBD_CONNECT_LAYOUTLOCK |
421                                   OBD_CONNECT_PINGLESS | OBD_CONNECT_LFSCK |
422                                   OBD_CONNECT_BULK_MBITS | OBD_CONNECT_SHORTIO |
423                                   OBD_CONNECT_FLAGS2 | OBD_CONNECT_GRANT_SHRINK;
424
425 /* The client currently advertises support for OBD_CONNECT_LOCKAHEAD_OLD so it
426  * can interoperate with an older version of lockahead which was released prior
427  * to landing in master. This support will be dropped when 2.13 development
428  * starts.  At the point, we should not just drop the connect flag (below), we
429  * should also remove the support in the code.
430  *
431  * Removing it means a few things:
432  * 1. Remove this section here
433  * 2. Remove CEF_NONBLOCK in ll_file_lockahead()
434  * 3. Remove function exp_connect_lockahead_old
435  * 4. Remove LDLM_FL_LOCKAHEAD_OLD_RESERVED in lustre_dlm_flags.h
436  * */
437 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 12, 50, 0)
438         data->ocd_connect_flags |= OBD_CONNECT_LOCKAHEAD_OLD;
439 #endif
440
441         data->ocd_connect_flags2 = OBD_CONNECT2_LOCKAHEAD;
442
443         if (!OBD_FAIL_CHECK(OBD_FAIL_OSC_CONNECT_GRANT_PARAM))
444                 data->ocd_connect_flags |= OBD_CONNECT_GRANT_PARAM;
445
446         /* OBD_CONNECT_CKSUM should always be set, even if checksums are
447          * disabled by default, because it can still be enabled on the
448          * fly via /sys. As a consequence, we still need to come to an
449          * agreement on the supported algorithms at connect time
450          */
451         data->ocd_connect_flags |= OBD_CONNECT_CKSUM;
452
453         if (OBD_FAIL_CHECK(OBD_FAIL_OSC_CKSUM_ADLER_ONLY))
454                 data->ocd_cksum_types = OBD_CKSUM_ADLER;
455         else
456                 data->ocd_cksum_types = obd_cksum_types_supported_client();
457
458 #ifdef HAVE_LRU_RESIZE_SUPPORT
459         data->ocd_connect_flags |= OBD_CONNECT_LRU_RESIZE;
460 #endif
461         /* always ping even if server suppress_pings */
462         if (sbi->ll_flags & LL_SBI_ALWAYS_PING)
463                 data->ocd_connect_flags &= ~OBD_CONNECT_PINGLESS;
464
465         CDEBUG(D_RPCTRACE, "ocd_connect_flags: %#llx ocd_version: %d "
466                "ocd_grant: %d\n", data->ocd_connect_flags,
467                data->ocd_version, data->ocd_grant);
468
469         sbi->ll_dt_obd->obd_upcall.onu_owner = &sbi->ll_lco;
470         sbi->ll_dt_obd->obd_upcall.onu_upcall = cl_ocd_update;
471
472         data->ocd_brw_size = DT_MAX_BRW_SIZE;
473
474         err = obd_connect(NULL, &sbi->ll_dt_exp, sbi->ll_dt_obd,
475                           &sbi->ll_sb_uuid, data, sbi->ll_cache);
476         if (err == -EBUSY) {
477                 LCONSOLE_ERROR_MSG(0x150, "An OST (dt %s) is performing "
478                                    "recovery, of which this client is not a "
479                                    "part.  Please wait for recovery to "
480                                    "complete, abort, or time out.\n", dt);
481                 GOTO(out_md, err);
482         } else if (err) {
483                 CERROR("%s: Cannot connect to %s: rc = %d\n",
484                        sbi->ll_dt_exp->exp_obd->obd_name, dt, err);
485                 GOTO(out_md, err);
486         }
487
488         sbi->ll_dt_exp->exp_connect_data = *data;
489
490         /* Don't change value if it was specified in the config log */
491         if (sbi->ll_ra_info.ra_max_read_ahead_whole_pages == -1) {
492                 sbi->ll_ra_info.ra_max_read_ahead_whole_pages =
493                         max_t(unsigned long, SBI_DEFAULT_READAHEAD_WHOLE_MAX,
494                               (data->ocd_brw_size >> PAGE_SHIFT));
495                 if (sbi->ll_ra_info.ra_max_read_ahead_whole_pages >
496                     sbi->ll_ra_info.ra_max_pages_per_file)
497                         sbi->ll_ra_info.ra_max_read_ahead_whole_pages =
498                                 sbi->ll_ra_info.ra_max_pages_per_file;
499         }
500
501         err = obd_fid_init(sbi->ll_dt_exp->exp_obd, sbi->ll_dt_exp,
502                            LUSTRE_SEQ_METADATA);
503         if (err) {
504                 CERROR("%s: Can't init data layer FID infrastructure, "
505                        "rc = %d\n", sbi->ll_dt_exp->exp_obd->obd_name, err);
506                 GOTO(out_dt, err);
507         }
508
509         mutex_lock(&sbi->ll_lco.lco_lock);
510         sbi->ll_lco.lco_flags = data->ocd_connect_flags;
511         sbi->ll_lco.lco_md_exp = sbi->ll_md_exp;
512         sbi->ll_lco.lco_dt_exp = sbi->ll_dt_exp;
513         mutex_unlock(&sbi->ll_lco.lco_lock);
514
515         fid_zero(&sbi->ll_root_fid);
516         err = md_get_root(sbi->ll_md_exp, get_mount_fileset(sb),
517                            &sbi->ll_root_fid);
518         if (err) {
519                 CERROR("cannot mds_connect: rc = %d\n", err);
520                 GOTO(out_lock_cn_cb, err);
521         }
522         if (!fid_is_sane(&sbi->ll_root_fid)) {
523                 CERROR("%s: Invalid root fid "DFID" during mount\n",
524                        sbi->ll_md_exp->exp_obd->obd_name,
525                        PFID(&sbi->ll_root_fid));
526                 GOTO(out_lock_cn_cb, err = -EINVAL);
527         }
528         CDEBUG(D_SUPER, "rootfid "DFID"\n", PFID(&sbi->ll_root_fid));
529
530         sb->s_op = &lustre_super_operations;
531 #ifdef HAVE_XATTR_HANDLER_FLAGS
532         sb->s_xattr = ll_xattr_handlers;
533 #endif
534 #if THREAD_SIZE >= 8192 /*b=17630*/
535         sb->s_export_op = &lustre_export_operations;
536 #endif
537
538         /* make root inode
539          * XXX: move this to after cbd setup? */
540         valid = OBD_MD_FLGETATTR | OBD_MD_FLBLOCKS | OBD_MD_FLMODEASIZE;
541         if (sbi->ll_flags & LL_SBI_ACL)
542                 valid |= OBD_MD_FLACL;
543
544         OBD_ALLOC_PTR(op_data);
545         if (op_data == NULL)
546                 GOTO(out_lock_cn_cb, err = -ENOMEM);
547
548         op_data->op_fid1 = sbi->ll_root_fid;
549         op_data->op_mode = 0;
550         op_data->op_valid = valid;
551
552         err = md_getattr(sbi->ll_md_exp, op_data, &request);
553
554         OBD_FREE_PTR(op_data);
555         if (err) {
556                 CERROR("%s: md_getattr failed for root: rc = %d\n",
557                        sbi->ll_md_exp->exp_obd->obd_name, err);
558                 GOTO(out_lock_cn_cb, err);
559         }
560
561         err = md_get_lustre_md(sbi->ll_md_exp, request, sbi->ll_dt_exp,
562                                sbi->ll_md_exp, &lmd);
563         if (err) {
564                 CERROR("failed to understand root inode md: rc = %d\n", err);
565                 ptlrpc_req_finished(request);
566                 GOTO(out_lock_cn_cb, err);
567         }
568
569         LASSERT(fid_is_sane(&sbi->ll_root_fid));
570         root = ll_iget(sb, cl_fid_build_ino(&sbi->ll_root_fid,
571                                             sbi->ll_flags & LL_SBI_32BIT_API),
572                        &lmd);
573         md_free_lustre_md(sbi->ll_md_exp, &lmd);
574         ptlrpc_req_finished(request);
575
576         if (IS_ERR(root)) {
577 #ifdef CONFIG_FS_POSIX_ACL
578                 if (lmd.posix_acl) {
579                         posix_acl_release(lmd.posix_acl);
580                         lmd.posix_acl = NULL;
581                 }
582 #endif
583                 err = IS_ERR(root) ? PTR_ERR(root) : -EBADF;
584                 root = NULL;
585                 CERROR("lustre_lite: bad iget4 for root\n");
586                 GOTO(out_root, err);
587         }
588
589         checksum = sbi->ll_flags & LL_SBI_CHECKSUM;
590         if (sbi->ll_checksum_set) {
591                 err = obd_set_info_async(NULL, sbi->ll_dt_exp,
592                                          sizeof(KEY_CHECKSUM), KEY_CHECKSUM,
593                                          sizeof(checksum), &checksum, NULL);
594                 if (err) {
595                         CERROR("%s: Set checksum failed: rc = %d\n",
596                                sbi->ll_dt_exp->exp_obd->obd_name, err);
597                         GOTO(out_root, err);
598                 }
599         }
600         cl_sb_init(sb);
601
602         sb->s_root = d_make_root(root);
603         if (sb->s_root == NULL) {
604                 err = -ENOMEM;
605                 CERROR("%s: can't make root dentry: rc = %d\n",
606                        sbi->ll_fsname, err);
607                 GOTO(out_root, err);
608         }
609 #ifdef HAVE_DCACHE_LOCK
610         sb->s_root->d_op = &ll_d_ops;
611 #endif
612
613         sbi->ll_sdev_orig = sb->s_dev;
614
615         /* We set sb->s_dev equal on all lustre clients in order to support
616          * NFS export clustering.  NFSD requires that the FSID be the same
617          * on all clients. */
618         /* s_dev is also used in lt_compare() to compare two fs, but that is
619          * only a node-local comparison. */
620         uuid = obd_get_uuid(sbi->ll_md_exp);
621         if (uuid != NULL)
622                 sb->s_dev = get_uuid2int(uuid->uuid, strlen(uuid->uuid));
623
624         if (data != NULL)
625                 OBD_FREE_PTR(data);
626         if (osfs != NULL)
627                 OBD_FREE_PTR(osfs);
628
629         if (sbi->ll_dt_obd) {
630                 err = sysfs_create_link(&sbi->ll_kset.kobj,
631                                         &sbi->ll_dt_obd->obd_kset.kobj,
632                                         sbi->ll_dt_obd->obd_type->typ_name);
633                 if (err < 0) {
634                         CERROR("%s: could not register %s in llite: rc = %d\n",
635                                dt, sbi->ll_fsname, err);
636                         err = 0;
637                 }
638         }
639
640         if (sbi->ll_md_obd) {
641                 err = sysfs_create_link(&sbi->ll_kset.kobj,
642                                         &sbi->ll_md_obd->obd_kset.kobj,
643                                         sbi->ll_md_obd->obd_type->typ_name);
644                 if (err < 0) {
645                         CERROR("%s: could not register %s in llite: rc = %d\n",
646                                md, sbi->ll_fsname, err);
647                         err = 0;
648                 }
649         }
650
651         RETURN(err);
652 out_root:
653         if (root)
654                 iput(root);
655 out_lock_cn_cb:
656         obd_fid_fini(sbi->ll_dt_exp->exp_obd);
657 out_dt:
658         obd_disconnect(sbi->ll_dt_exp);
659         sbi->ll_dt_exp = NULL;
660         sbi->ll_dt_obd = NULL;
661 out_md_fid:
662         obd_fid_fini(sbi->ll_md_exp->exp_obd);
663 out_md:
664         obd_disconnect(sbi->ll_md_exp);
665         sbi->ll_md_exp = NULL;
666         sbi->ll_md_obd = NULL;
667 out:
668         if (data != NULL)
669                 OBD_FREE_PTR(data);
670         if (osfs != NULL)
671                 OBD_FREE_PTR(osfs);
672         return err;
673 }
674
675 int ll_get_max_mdsize(struct ll_sb_info *sbi, int *lmmsize)
676 {
677         int size, rc;
678
679         size = sizeof(*lmmsize);
680         rc = obd_get_info(NULL, sbi->ll_dt_exp, sizeof(KEY_MAX_EASIZE),
681                           KEY_MAX_EASIZE, &size, lmmsize);
682         if (rc != 0) {
683                 CERROR("%s: cannot get max LOV EA size: rc = %d\n",
684                        sbi->ll_dt_exp->exp_obd->obd_name, rc);
685                 RETURN(rc);
686         }
687
688         CDEBUG(D_INFO, "max LOV ea size: %d\n", *lmmsize);
689
690         size = sizeof(int);
691         rc = obd_get_info(NULL, sbi->ll_md_exp, sizeof(KEY_MAX_EASIZE),
692                           KEY_MAX_EASIZE, &size, lmmsize);
693         if (rc)
694                 CERROR("Get max mdsize error rc %d\n", rc);
695
696         CDEBUG(D_INFO, "max LMV ea size: %d\n", *lmmsize);
697
698         RETURN(rc);
699 }
700
701 /**
702  * Get the value of the default_easize parameter.
703  *
704  * \see client_obd::cl_default_mds_easize
705  *
706  * \param[in] sbi       superblock info for this filesystem
707  * \param[out] lmmsize  pointer to storage location for value
708  *
709  * \retval 0            on success
710  * \retval negative     negated errno on failure
711  */
712 int ll_get_default_mdsize(struct ll_sb_info *sbi, int *lmmsize)
713 {
714         int size, rc;
715
716         size = sizeof(int);
717         rc = obd_get_info(NULL, sbi->ll_md_exp, sizeof(KEY_DEFAULT_EASIZE),
718                          KEY_DEFAULT_EASIZE, &size, lmmsize);
719         if (rc)
720                 CERROR("Get default mdsize error rc %d\n", rc);
721
722         RETURN(rc);
723 }
724
725 /**
726  * Set the default_easize parameter to the given value.
727  *
728  * \see client_obd::cl_default_mds_easize
729  *
730  * \param[in] sbi       superblock info for this filesystem
731  * \param[in] lmmsize   the size to set
732  *
733  * \retval 0            on success
734  * \retval negative     negated errno on failure
735  */
736 int ll_set_default_mdsize(struct ll_sb_info *sbi, int lmmsize)
737 {
738         int rc;
739
740         if (lmmsize < sizeof(struct lov_mds_md) ||
741             lmmsize > OBD_MAX_DEFAULT_EA_SIZE)
742                 return -EINVAL;
743
744         rc = obd_set_info_async(NULL, sbi->ll_md_exp,
745                                 sizeof(KEY_DEFAULT_EASIZE), KEY_DEFAULT_EASIZE,
746                                 sizeof(int), &lmmsize, NULL);
747
748         RETURN(rc);
749 }
750
751 static void client_common_put_super(struct super_block *sb)
752 {
753         struct ll_sb_info *sbi = ll_s2sbi(sb);
754         ENTRY;
755
756         cl_sb_fini(sb);
757
758         obd_fid_fini(sbi->ll_dt_exp->exp_obd);
759         obd_disconnect(sbi->ll_dt_exp);
760         sbi->ll_dt_exp = NULL;
761
762         ll_debugfs_unregister_super(sb);
763
764         obd_fid_fini(sbi->ll_md_exp->exp_obd);
765         obd_disconnect(sbi->ll_md_exp);
766         sbi->ll_md_exp = NULL;
767
768         EXIT;
769 }
770
771 void ll_kill_super(struct super_block *sb)
772 {
773         struct ll_sb_info *sbi;
774         ENTRY;
775
776         /* not init sb ?*/
777         if (!(sb->s_flags & MS_ACTIVE))
778                 return;
779
780         sbi = ll_s2sbi(sb);
781         /* we need restore s_dev from changed for clustred NFS before put_super
782          * because new kernels have cached s_dev and change sb->s_dev in
783          * put_super not affected real removing devices */
784         if (sbi) {
785                 sb->s_dev = sbi->ll_sdev_orig;
786                 sbi->ll_umounting = 1;
787
788                 /* wait running statahead threads to quit */
789                 while (atomic_read(&sbi->ll_sa_running) > 0) {
790                         set_current_state(TASK_UNINTERRUPTIBLE);
791                         schedule_timeout(msecs_to_jiffies(MSEC_PER_SEC >> 3));
792                 }
793         }
794
795         EXIT;
796 }
797
798 static inline int ll_set_opt(const char *opt, char *data, int fl)
799 {
800         if (strncmp(opt, data, strlen(opt)) != 0)
801                 return 0;
802         else
803                 return fl;
804 }
805
806 /* non-client-specific mount options are parsed in lmd_parse */
807 static int ll_options(char *options, struct ll_sb_info *sbi)
808 {
809         int tmp;
810         char *s1 = options, *s2;
811         int *flags = &sbi->ll_flags;
812         ENTRY;
813
814         if (!options)
815                 RETURN(0);
816
817         CDEBUG(D_CONFIG, "Parsing opts %s\n", options);
818
819         while (*s1) {
820                 CDEBUG(D_SUPER, "next opt=%s\n", s1);
821                 tmp = ll_set_opt("nolock", s1, LL_SBI_NOLCK);
822                 if (tmp) {
823                         *flags |= tmp;
824                         goto next;
825                 }
826                 tmp = ll_set_opt("flock", s1, LL_SBI_FLOCK);
827                 if (tmp) {
828                         *flags |= tmp;
829                         goto next;
830                 }
831                 tmp = ll_set_opt("localflock", s1, LL_SBI_LOCALFLOCK);
832                 if (tmp) {
833                         *flags |= tmp;
834                         goto next;
835                 }
836                 tmp = ll_set_opt("noflock", s1, LL_SBI_FLOCK|LL_SBI_LOCALFLOCK);
837                 if (tmp) {
838                         *flags &= ~tmp;
839                         goto next;
840                 }
841                 tmp = ll_set_opt("user_xattr", s1, LL_SBI_USER_XATTR);
842                 if (tmp) {
843                         *flags |= tmp;
844                         goto next;
845                 }
846                 tmp = ll_set_opt("nouser_xattr", s1, LL_SBI_USER_XATTR);
847                 if (tmp) {
848                         *flags &= ~tmp;
849                         goto next;
850                 }
851                 tmp = ll_set_opt("context", s1, 1);
852                 if (tmp)
853                         goto next;
854                 tmp = ll_set_opt("fscontext", s1, 1);
855                 if (tmp)
856                         goto next;
857                 tmp = ll_set_opt("defcontext", s1, 1);
858                 if (tmp)
859                         goto next;
860                 tmp = ll_set_opt("rootcontext", s1, 1);
861                 if (tmp)
862                         goto next;
863                 tmp = ll_set_opt("user_fid2path", s1, LL_SBI_USER_FID2PATH);
864                 if (tmp) {
865                         *flags |= tmp;
866                         goto next;
867                 }
868                 tmp = ll_set_opt("nouser_fid2path", s1, LL_SBI_USER_FID2PATH);
869                 if (tmp) {
870                         *flags &= ~tmp;
871                         goto next;
872                 }
873
874                 tmp = ll_set_opt("checksum", s1, LL_SBI_CHECKSUM);
875                 if (tmp) {
876                         *flags |= tmp;
877                         sbi->ll_checksum_set = 1;
878                         goto next;
879                 }
880                 tmp = ll_set_opt("nochecksum", s1, LL_SBI_CHECKSUM);
881                 if (tmp) {
882                         *flags &= ~tmp;
883                         sbi->ll_checksum_set = 1;
884                         goto next;
885                 }
886                 tmp = ll_set_opt("lruresize", s1, LL_SBI_LRU_RESIZE);
887                 if (tmp) {
888                         *flags |= tmp;
889                         goto next;
890                 }
891                 tmp = ll_set_opt("nolruresize", s1, LL_SBI_LRU_RESIZE);
892                 if (tmp) {
893                         *flags &= ~tmp;
894                         goto next;
895                 }
896                 tmp = ll_set_opt("lazystatfs", s1, LL_SBI_LAZYSTATFS);
897                 if (tmp) {
898                         *flags |= tmp;
899                         goto next;
900                 }
901                 tmp = ll_set_opt("nolazystatfs", s1, LL_SBI_LAZYSTATFS);
902                 if (tmp) {
903                         *flags &= ~tmp;
904                         goto next;
905                 }
906                 tmp = ll_set_opt("32bitapi", s1, LL_SBI_32BIT_API);
907                 if (tmp) {
908                         *flags |= tmp;
909                         goto next;
910                 }
911                 tmp = ll_set_opt("verbose", s1, LL_SBI_VERBOSE);
912                 if (tmp) {
913                         *flags |= tmp;
914                         goto next;
915                 }
916                 tmp = ll_set_opt("noverbose", s1, LL_SBI_VERBOSE);
917                 if (tmp) {
918                         *flags &= ~tmp;
919                         goto next;
920                 }
921                 tmp = ll_set_opt("always_ping", s1, LL_SBI_ALWAYS_PING);
922                 if (tmp) {
923                         *flags |= tmp;
924                         goto next;
925                 }
926                 LCONSOLE_ERROR_MSG(0x152, "Unknown option '%s', won't mount.\n",
927                                    s1);
928                 RETURN(-EINVAL);
929
930 next:
931                 /* Find next opt */
932                 s2 = strchr(s1, ',');
933                 if (s2 == NULL)
934                         break;
935                 s1 = s2 + 1;
936         }
937         RETURN(0);
938 }
939
940 void ll_lli_init(struct ll_inode_info *lli)
941 {
942         lli->lli_inode_magic = LLI_INODE_MAGIC;
943         lli->lli_flags = 0;
944         spin_lock_init(&lli->lli_lock);
945         lli->lli_posix_acl = NULL;
946         /* Do not set lli_fid, it has been initialized already. */
947         fid_zero(&lli->lli_pfid);
948         lli->lli_mds_read_och = NULL;
949         lli->lli_mds_write_och = NULL;
950         lli->lli_mds_exec_och = NULL;
951         lli->lli_open_fd_read_count = 0;
952         lli->lli_open_fd_write_count = 0;
953         lli->lli_open_fd_exec_count = 0;
954         mutex_init(&lli->lli_och_mutex);
955         spin_lock_init(&lli->lli_agl_lock);
956         spin_lock_init(&lli->lli_layout_lock);
957         ll_layout_version_set(lli, CL_LAYOUT_GEN_NONE);
958         lli->lli_clob = NULL;
959
960         init_rwsem(&lli->lli_xattrs_list_rwsem);
961         mutex_init(&lli->lli_xattrs_enq_lock);
962
963         LASSERT(lli->lli_vfs_inode.i_mode != 0);
964         if (S_ISDIR(lli->lli_vfs_inode.i_mode)) {
965                 mutex_init(&lli->lli_readdir_mutex);
966                 lli->lli_opendir_key = NULL;
967                 lli->lli_sai = NULL;
968                 spin_lock_init(&lli->lli_sa_lock);
969                 lli->lli_opendir_pid = 0;
970                 lli->lli_sa_enabled = 0;
971                 lli->lli_def_stripe_offset = -1;
972                 init_rwsem(&lli->lli_lsm_sem);
973         } else {
974                 mutex_init(&lli->lli_size_mutex);
975                 lli->lli_symlink_name = NULL;
976                 init_rwsem(&lli->lli_trunc_sem);
977                 range_lock_tree_init(&lli->lli_write_tree);
978                 init_rwsem(&lli->lli_glimpse_sem);
979                 lli->lli_glimpse_time = ktime_set(0, 0);
980                 INIT_LIST_HEAD(&lli->lli_agl_list);
981                 lli->lli_agl_index = 0;
982                 lli->lli_async_rc = 0;
983                 spin_lock_init(&lli->lli_heat_lock);
984                 obd_heat_clear(lli->lli_heat_instances, OBD_HEAT_COUNT);
985                 lli->lli_heat_flags = 0;
986                 mutex_init(&lli->lli_pcc_lock);
987                 lli->lli_pcc_state = PCC_STATE_FL_NONE;
988                 lli->lli_pcc_inode = NULL;
989         }
990         mutex_init(&lli->lli_layout_mutex);
991         memset(lli->lli_jobid, 0, sizeof(lli->lli_jobid));
992 }
993
994 #define MAX_STRING_SIZE 128
995
996 #ifndef HAVE_SUPER_SETUP_BDI_NAME
997
998 #define LSI_BDI_INITIALIZED     0x00400000
999
1000 #ifndef HAVE_BDI_CAP_MAP_COPY
1001 # define BDI_CAP_MAP_COPY       0
1002 #endif
1003
1004 static int super_setup_bdi_name(struct super_block *sb, char *fmt, ...)
1005 {
1006         struct  lustre_sb_info *lsi = s2lsi(sb);
1007         char buf[MAX_STRING_SIZE];
1008         va_list args;
1009         int err;
1010
1011         err = bdi_init(&lsi->lsi_bdi);
1012         if (err)
1013                 return err;
1014
1015         lsi->lsi_flags |= LSI_BDI_INITIALIZED;
1016         lsi->lsi_bdi.capabilities = BDI_CAP_MAP_COPY;
1017         lsi->lsi_bdi.name = "lustre";
1018         va_start(args, fmt);
1019         vsnprintf(buf, MAX_STRING_SIZE, fmt, args);
1020         va_end(args);
1021         err = bdi_register(&lsi->lsi_bdi, NULL, "%s", buf);
1022         va_end(args);
1023         if (!err)
1024                 sb->s_bdi = &lsi->lsi_bdi;
1025
1026         return err;
1027 }
1028 #endif /* !HAVE_SUPER_SETUP_BDI_NAME */
1029
1030 int ll_fill_super(struct super_block *sb, struct vfsmount *mnt)
1031 {
1032         struct  lustre_profile *lprof = NULL;
1033         struct  lustre_sb_info *lsi = s2lsi(sb);
1034         struct  ll_sb_info *sbi = NULL;
1035         char    *dt = NULL, *md = NULL;
1036         char    *profilenm = get_profile_name(sb);
1037         struct config_llog_instance *cfg;
1038         /* %p for void* in printf needs 16+2 characters: 0xffffffffffffffff */
1039         const int instlen = 16 + 2;
1040         unsigned long cfg_instance = ll_get_cfg_instance(sb);
1041         char name[MAX_STRING_SIZE];
1042         int md_len = 0;
1043         int dt_len = 0;
1044         uuid_t uuid;
1045         char *ptr;
1046         int len;
1047         int err;
1048
1049         ENTRY;
1050         /* for ASLR, to map between cfg_instance and hashed ptr */
1051         CDEBUG(D_VFSTRACE, "VFS Op: cfg_instance %s-%016lx (sb %p)\n",
1052                profilenm, cfg_instance, sb);
1053
1054         try_module_get(THIS_MODULE);
1055
1056         OBD_ALLOC_PTR(cfg);
1057         if (cfg == NULL)
1058                 GOTO(out_free_cfg, err = -ENOMEM);
1059
1060         /* client additional sb info */
1061         lsi->lsi_llsbi = sbi = ll_init_sbi();
1062         if (!sbi)
1063                 GOTO(out_free_cfg, err = -ENOMEM);
1064
1065         err = ll_options(lsi->lsi_lmd->lmd_opts, sbi);
1066         if (err)
1067                 GOTO(out_free_cfg, err);
1068
1069 #ifndef HAVE_DCACHE_LOCK
1070         /* kernel >= 2.6.38 store dentry operations in sb->s_d_op. */
1071         sb->s_d_op = &ll_d_ops;
1072 #endif
1073         /* UUID handling */
1074         generate_random_uuid(uuid.b);
1075         snprintf(sbi->ll_sb_uuid.uuid, UUID_SIZE, "%pU", uuid.b);
1076
1077         CDEBUG(D_CONFIG, "llite sb uuid: %s\n", sbi->ll_sb_uuid.uuid);
1078
1079         /* Get fsname */
1080         len = strlen(profilenm);
1081         ptr = strrchr(profilenm, '-');
1082         if (ptr && (strcmp(ptr, "-client") == 0))
1083                 len -= 7;
1084
1085         if (len > LUSTRE_MAXFSNAME) {
1086                 if (unlikely(len >= MAX_STRING_SIZE))
1087                         len = MAX_STRING_SIZE - 1;
1088                 strncpy(name, profilenm, len);
1089                 name[len] = '\0';
1090                 err = -ENAMETOOLONG;
1091                 CERROR("%s: fsname longer than %u characters: rc = %d\n",
1092                        name, LUSTRE_MAXFSNAME, err);
1093                 GOTO(out_free_cfg, err);
1094         }
1095         strncpy(sbi->ll_fsname, profilenm, len);
1096         sbi->ll_fsname[len] = '\0';
1097
1098         /* Mount info */
1099         snprintf(name, sizeof(name), "%.*s-%016lx", len,
1100                  profilenm, cfg_instance);
1101
1102         err = super_setup_bdi_name(sb, "%s", name);
1103         if (err)
1104                 GOTO(out_free_cfg, err);
1105
1106         /* Call ll_debugfs_register_super() before lustre_process_log()
1107          * so that "llite.*.*" params can be processed correctly.
1108          */
1109         err = ll_debugfs_register_super(sb, name);
1110         if (err < 0) {
1111                 CERROR("%s: could not register mountpoint in llite: rc = %d\n",
1112                        sbi->ll_fsname, err);
1113                 err = 0;
1114         }
1115
1116         /* The cfg_instance is a value unique to this super, in case some
1117          * joker tries to mount the same fs at two mount points.
1118          */
1119         cfg->cfg_instance = cfg_instance;
1120         cfg->cfg_uuid = lsi->lsi_llsbi->ll_sb_uuid;
1121         cfg->cfg_callback = class_config_llog_handler;
1122         cfg->cfg_sub_clds = CONFIG_SUB_CLIENT;
1123         /* set up client obds */
1124         err = lustre_process_log(sb, profilenm, cfg);
1125         if (err < 0)
1126                 GOTO(out_debugfs, err);
1127
1128         /* Profile set with LCFG_MOUNTOPT so we can find our mdc and osc obds */
1129         lprof = class_get_profile(profilenm);
1130         if (lprof == NULL) {
1131                 LCONSOLE_ERROR_MSG(0x156, "The client profile '%s' could not be"
1132                                    " read from the MGS.  Does that filesystem "
1133                                    "exist?\n", profilenm);
1134                 GOTO(out_debugfs, err = -EINVAL);
1135         }
1136         CDEBUG(D_CONFIG, "Found profile %s: mdc=%s osc=%s\n", profilenm,
1137                lprof->lp_md, lprof->lp_dt);
1138
1139         dt_len = strlen(lprof->lp_dt) + instlen + 2;
1140         OBD_ALLOC(dt, dt_len);
1141         if (!dt)
1142                 GOTO(out_profile, err = -ENOMEM);
1143         snprintf(dt, dt_len - 1, "%s-%016lx", lprof->lp_dt, cfg_instance);
1144
1145         md_len = strlen(lprof->lp_md) + instlen + 2;
1146         OBD_ALLOC(md, md_len);
1147         if (!md)
1148                 GOTO(out_free_dt, err = -ENOMEM);
1149         snprintf(md, md_len - 1, "%s-%016lx", lprof->lp_md, cfg_instance);
1150
1151         /* connections, registrations, sb setup */
1152         err = client_common_fill_super(sb, md, dt, mnt);
1153         if (err < 0)
1154                 GOTO(out_free_md, err);
1155
1156         sbi->ll_client_common_fill_super_succeeded = 1;
1157
1158 out_free_md:
1159         if (md)
1160                 OBD_FREE(md, md_len);
1161 out_free_dt:
1162         if (dt)
1163                 OBD_FREE(dt, dt_len);
1164 out_profile:
1165         if (lprof)
1166                 class_put_profile(lprof);
1167 out_debugfs:
1168         if (err < 0)
1169                 ll_debugfs_unregister_super(sb);
1170 out_free_cfg:
1171         if (cfg)
1172                 OBD_FREE_PTR(cfg);
1173
1174         if (err)
1175                 ll_put_super(sb);
1176         else if (sbi->ll_flags & LL_SBI_VERBOSE)
1177                 LCONSOLE_WARN("Mounted %s\n", profilenm);
1178         RETURN(err);
1179 } /* ll_fill_super */
1180
1181 void ll_put_super(struct super_block *sb)
1182 {
1183         struct config_llog_instance cfg, params_cfg;
1184         struct obd_device *obd;
1185         struct lustre_sb_info *lsi = s2lsi(sb);
1186         struct ll_sb_info *sbi = ll_s2sbi(sb);
1187         char *profilenm = get_profile_name(sb);
1188         unsigned long cfg_instance = ll_get_cfg_instance(sb);
1189         long ccc_count;
1190         int next, force = 1, rc = 0;
1191         ENTRY;
1192
1193         if (!sbi)
1194                 GOTO(out_no_sbi, 0);
1195
1196         /* Should replace instance_id with something better for ASLR */
1197         CDEBUG(D_VFSTRACE, "VFS Op: cfg_instance %s-%016lx (sb %p)\n",
1198                profilenm, cfg_instance, sb);
1199
1200         cfg.cfg_instance = cfg_instance;
1201         lustre_end_log(sb, profilenm, &cfg);
1202
1203         params_cfg.cfg_instance = cfg_instance;
1204         lustre_end_log(sb, PARAMS_FILENAME, &params_cfg);
1205
1206         if (sbi->ll_md_exp) {
1207                 obd = class_exp2obd(sbi->ll_md_exp);
1208                 if (obd)
1209                         force = obd->obd_force;
1210         }
1211
1212         /* Wait for unstable pages to be committed to stable storage */
1213         if (force == 0) {
1214                 struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
1215                 rc = l_wait_event(sbi->ll_cache->ccc_unstable_waitq,
1216                         atomic_long_read(&sbi->ll_cache->ccc_unstable_nr) == 0,
1217                         &lwi);
1218         }
1219
1220         ccc_count = atomic_long_read(&sbi->ll_cache->ccc_unstable_nr);
1221         if (force == 0 && rc != -EINTR)
1222                 LASSERTF(ccc_count == 0, "count: %li\n", ccc_count);
1223
1224         /* We need to set force before the lov_disconnect in
1225            lustre_common_put_super, since l_d cleans up osc's as well. */
1226         if (force) {
1227                 next = 0;
1228                 while ((obd = class_devices_in_group(&sbi->ll_sb_uuid,
1229                                                      &next)) != NULL) {
1230                         obd->obd_force = force;
1231                 }
1232         }
1233
1234         if (sbi->ll_client_common_fill_super_succeeded) {
1235                 /* Only if client_common_fill_super succeeded */
1236                 client_common_put_super(sb);
1237         }
1238
1239         next = 0;
1240         while ((obd = class_devices_in_group(&sbi->ll_sb_uuid, &next)) !=NULL) {
1241                 class_manual_cleanup(obd);
1242         }
1243
1244         if (sbi->ll_flags & LL_SBI_VERBOSE)
1245                 LCONSOLE_WARN("Unmounted %s\n", profilenm ? profilenm : "");
1246
1247         if (profilenm)
1248                 class_del_profile(profilenm);
1249
1250 #ifndef HAVE_SUPER_SETUP_BDI_NAME
1251         if (lsi->lsi_flags & LSI_BDI_INITIALIZED) {
1252                 bdi_destroy(&lsi->lsi_bdi);
1253                 lsi->lsi_flags &= ~LSI_BDI_INITIALIZED;
1254         }
1255 #endif
1256
1257         ll_free_sbi(sb);
1258         lsi->lsi_llsbi = NULL;
1259 out_no_sbi:
1260         lustre_common_put_super(sb);
1261
1262         cl_env_cache_purge(~0);
1263
1264         module_put(THIS_MODULE);
1265
1266         EXIT;
1267 } /* client_put_super */
1268
1269 struct inode *ll_inode_from_resource_lock(struct ldlm_lock *lock)
1270 {
1271         struct inode *inode = NULL;
1272
1273         /* NOTE: we depend on atomic igrab() -bzzz */
1274         lock_res_and_lock(lock);
1275         if (lock->l_resource->lr_lvb_inode) {
1276                 struct ll_inode_info * lli;
1277                 lli = ll_i2info(lock->l_resource->lr_lvb_inode);
1278                 if (lli->lli_inode_magic == LLI_INODE_MAGIC) {
1279                         inode = igrab(lock->l_resource->lr_lvb_inode);
1280                 } else {
1281                         inode = lock->l_resource->lr_lvb_inode;
1282                         LDLM_DEBUG_LIMIT(inode->i_state & I_FREEING ?  D_INFO :
1283                                          D_WARNING, lock, "lr_lvb_inode %p is "
1284                                          "bogus: magic %08x",
1285                                          lock->l_resource->lr_lvb_inode,
1286                                          lli->lli_inode_magic);
1287                         inode = NULL;
1288                 }
1289         }
1290         unlock_res_and_lock(lock);
1291         return inode;
1292 }
1293
1294 void ll_dir_clear_lsm_md(struct inode *inode)
1295 {
1296         struct ll_inode_info *lli = ll_i2info(inode);
1297
1298         LASSERT(S_ISDIR(inode->i_mode));
1299
1300         if (lli->lli_lsm_md != NULL) {
1301                 lmv_free_memmd(lli->lli_lsm_md);
1302                 lli->lli_lsm_md = NULL;
1303         }
1304 }
1305
1306 static struct inode *ll_iget_anon_dir(struct super_block *sb,
1307                                       const struct lu_fid *fid,
1308                                       struct lustre_md *md)
1309 {
1310         struct ll_sb_info       *sbi = ll_s2sbi(sb);
1311         struct mdt_body         *body = md->body;
1312         struct inode            *inode;
1313         ino_t                   ino;
1314         ENTRY;
1315
1316         ino = cl_fid_build_ino(fid, sbi->ll_flags & LL_SBI_32BIT_API);
1317         inode = iget_locked(sb, ino);
1318         if (inode == NULL) {
1319                 CERROR("%s: failed get simple inode "DFID": rc = -ENOENT\n",
1320                        sbi->ll_fsname, PFID(fid));
1321                 RETURN(ERR_PTR(-ENOENT));
1322         }
1323
1324         if (inode->i_state & I_NEW) {
1325                 struct ll_inode_info *lli = ll_i2info(inode);
1326                 struct lmv_stripe_md *lsm = md->lmv;
1327
1328                 inode->i_mode = (inode->i_mode & ~S_IFMT) |
1329                                 (body->mbo_mode & S_IFMT);
1330                 LASSERTF(S_ISDIR(inode->i_mode), "Not slave inode "DFID"\n",
1331                          PFID(fid));
1332
1333                 inode->i_mtime.tv_sec = 0;
1334                 inode->i_atime.tv_sec = 0;
1335                 inode->i_ctime.tv_sec = 0;
1336                 inode->i_rdev = 0;
1337
1338 #ifdef HAVE_BACKING_DEV_INFO
1339                 /* initializing backing dev info. */
1340                 inode->i_mapping->backing_dev_info =
1341                                                 &s2lsi(inode->i_sb)->lsi_bdi;
1342 #endif
1343                 inode->i_op = &ll_dir_inode_operations;
1344                 inode->i_fop = &ll_dir_operations;
1345                 lli->lli_fid = *fid;
1346                 ll_lli_init(lli);
1347
1348                 LASSERT(lsm != NULL);
1349                 /* master object FID */
1350                 lli->lli_pfid = body->mbo_fid1;
1351                 CDEBUG(D_INODE, "lli %p slave "DFID" master "DFID"\n",
1352                        lli, PFID(fid), PFID(&lli->lli_pfid));
1353                 unlock_new_inode(inode);
1354         }
1355
1356         RETURN(inode);
1357 }
1358
1359 static int ll_init_lsm_md(struct inode *inode, struct lustre_md *md)
1360 {
1361         struct lu_fid *fid;
1362         struct lmv_stripe_md *lsm = md->lmv;
1363         struct ll_inode_info *lli = ll_i2info(inode);
1364         int i;
1365
1366         LASSERT(lsm != NULL);
1367
1368         CDEBUG(D_INODE, "%s: "DFID" set dir layout:\n",
1369                ll_i2sbi(inode)->ll_fsname, PFID(&lli->lli_fid));
1370         lsm_md_dump(D_INODE, lsm);
1371
1372         /* XXX sigh, this lsm_root initialization should be in
1373          * LMV layer, but it needs ll_iget right now, so we
1374          * put this here right now. */
1375         for (i = 0; i < lsm->lsm_md_stripe_count; i++) {
1376                 fid = &lsm->lsm_md_oinfo[i].lmo_fid;
1377                 LASSERT(lsm->lsm_md_oinfo[i].lmo_root == NULL);
1378
1379                 if (!fid_is_sane(fid))
1380                         continue;
1381
1382                 /* Unfortunately ll_iget will call ll_update_inode,
1383                  * where the initialization of slave inode is slightly
1384                  * different, so it reset lsm_md to NULL to avoid
1385                  * initializing lsm for slave inode. */
1386                 lsm->lsm_md_oinfo[i].lmo_root =
1387                                 ll_iget_anon_dir(inode->i_sb, fid, md);
1388                 if (IS_ERR(lsm->lsm_md_oinfo[i].lmo_root)) {
1389                         int rc = PTR_ERR(lsm->lsm_md_oinfo[i].lmo_root);
1390
1391                         lsm->lsm_md_oinfo[i].lmo_root = NULL;
1392                         while (i-- > 0) {
1393                                 iput(lsm->lsm_md_oinfo[i].lmo_root);
1394                                 lsm->lsm_md_oinfo[i].lmo_root = NULL;
1395                         }
1396                         return rc;
1397                 }
1398         }
1399
1400         lli->lli_lsm_md = lsm;
1401
1402         return 0;
1403 }
1404
1405 static int ll_update_lsm_md(struct inode *inode, struct lustre_md *md)
1406 {
1407         struct ll_inode_info *lli = ll_i2info(inode);
1408         struct lmv_stripe_md *lsm = md->lmv;
1409         int rc = 0;
1410
1411         ENTRY;
1412
1413         LASSERT(S_ISDIR(inode->i_mode));
1414         CDEBUG(D_INODE, "update lsm %p of "DFID"\n", lli->lli_lsm_md,
1415                PFID(ll_inode2fid(inode)));
1416
1417         /*
1418          * no striped information from request, lustre_md from req does not
1419          * include stripeEA, see ll_md_setattr()
1420          */
1421         if (!lsm)
1422                 RETURN(0);
1423
1424         /*
1425          * normally dir layout doesn't change, only take read lock to check
1426          * that to avoid blocking other MD operations.
1427          */
1428         if (lli->lli_lsm_md)
1429                 down_read(&lli->lli_lsm_sem);
1430         else
1431                 down_write(&lli->lli_lsm_sem);
1432
1433         /*
1434          * if dir layout mismatch, check whether version is increased, which
1435          * means layout is changed, this happens in dir migration and lfsck.
1436          *
1437          * foreign LMV should not change.
1438          */
1439         if (lli->lli_lsm_md &&
1440             lli->lli_lsm_md->lsm_md_magic != LMV_MAGIC_FOREIGN &&
1441            !lsm_md_eq(lli->lli_lsm_md, lsm)) {
1442                 if (lsm->lsm_md_layout_version <=
1443                     lli->lli_lsm_md->lsm_md_layout_version) {
1444                         CERROR("%s: "DFID" dir layout mismatch:\n",
1445                                ll_i2sbi(inode)->ll_fsname,
1446                                PFID(&lli->lli_fid));
1447                         lsm_md_dump(D_ERROR, lli->lli_lsm_md);
1448                         lsm_md_dump(D_ERROR, lsm);
1449                         GOTO(unlock, rc = -EINVAL);
1450                 }
1451
1452                 /* layout changed, switch to write lock */
1453                 up_read(&lli->lli_lsm_sem);
1454                 down_write(&lli->lli_lsm_sem);
1455                 ll_dir_clear_lsm_md(inode);
1456         }
1457
1458         /* set directory layout */
1459         if (!lli->lli_lsm_md) {
1460                 struct cl_attr  *attr;
1461
1462                 if (lsm->lsm_md_magic == LMV_MAGIC_FOREIGN) {
1463                         /* set md->lmv to NULL, so the following free lustre_md
1464                          * will not free this lsm */
1465                         md->lmv = NULL;
1466                         lli->lli_lsm_md = lsm;
1467                         up_write(&lli->lli_lsm_sem);
1468                         RETURN(0);
1469                 }
1470
1471                 rc = ll_init_lsm_md(inode, md);
1472                 up_write(&lli->lli_lsm_sem);
1473                 if (rc != 0)
1474                         RETURN(rc);
1475
1476                 /* set md->lmv to NULL, so the following free lustre_md
1477                  * will not free this lsm */
1478                 md->lmv = NULL;
1479
1480                 /*
1481                  * md_merge_attr() may take long, since lsm is already set,
1482                  * switch to read lock.
1483                  */
1484                 down_read(&lli->lli_lsm_sem);
1485
1486                 OBD_ALLOC_PTR(attr);
1487                 if (attr == NULL)
1488                         GOTO(unlock, rc = -ENOMEM);
1489
1490                 /* validate the lsm */
1491                 rc = md_merge_attr(ll_i2mdexp(inode), lsm, attr,
1492                                    ll_md_blocking_ast);
1493                 if (rc != 0) {
1494                         OBD_FREE_PTR(attr);
1495                         GOTO(unlock, rc);
1496                 }
1497
1498                 if (md->body->mbo_valid & OBD_MD_FLNLINK)
1499                         md->body->mbo_nlink = attr->cat_nlink;
1500                 if (md->body->mbo_valid & OBD_MD_FLSIZE)
1501                         md->body->mbo_size = attr->cat_size;
1502                 if (md->body->mbo_valid & OBD_MD_FLATIME)
1503                         md->body->mbo_atime = attr->cat_atime;
1504                 if (md->body->mbo_valid & OBD_MD_FLCTIME)
1505                         md->body->mbo_ctime = attr->cat_ctime;
1506                 if (md->body->mbo_valid & OBD_MD_FLMTIME)
1507                         md->body->mbo_mtime = attr->cat_mtime;
1508
1509                 OBD_FREE_PTR(attr);
1510         }
1511 unlock:
1512         up_read(&lli->lli_lsm_sem);
1513
1514         RETURN(rc);
1515 }
1516
1517 void ll_clear_inode(struct inode *inode)
1518 {
1519         struct ll_inode_info *lli = ll_i2info(inode);
1520         struct ll_sb_info *sbi = ll_i2sbi(inode);
1521
1522         ENTRY;
1523
1524         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
1525                PFID(ll_inode2fid(inode)), inode);
1526
1527         if (S_ISDIR(inode->i_mode)) {
1528                 /* these should have been cleared in ll_file_release */
1529                 LASSERT(lli->lli_opendir_key == NULL);
1530                 LASSERT(lli->lli_sai == NULL);
1531                 LASSERT(lli->lli_opendir_pid == 0);
1532         } else {
1533                 pcc_inode_free(inode);
1534         }
1535
1536         md_null_inode(sbi->ll_md_exp, ll_inode2fid(inode));
1537
1538         LASSERT(!lli->lli_open_fd_write_count);
1539         LASSERT(!lli->lli_open_fd_read_count);
1540         LASSERT(!lli->lli_open_fd_exec_count);
1541
1542         if (lli->lli_mds_write_och)
1543                 ll_md_real_close(inode, FMODE_WRITE);
1544         if (lli->lli_mds_exec_och)
1545                 ll_md_real_close(inode, FMODE_EXEC);
1546         if (lli->lli_mds_read_och)
1547                 ll_md_real_close(inode, FMODE_READ);
1548
1549         if (S_ISLNK(inode->i_mode) && lli->lli_symlink_name) {
1550                 OBD_FREE(lli->lli_symlink_name,
1551                          strlen(lli->lli_symlink_name) + 1);
1552                 lli->lli_symlink_name = NULL;
1553         }
1554
1555         ll_xattr_cache_destroy(inode);
1556
1557 #ifdef CONFIG_FS_POSIX_ACL
1558         forget_all_cached_acls(inode);
1559         if (lli->lli_posix_acl) {
1560                 posix_acl_release(lli->lli_posix_acl);
1561                 lli->lli_posix_acl = NULL;
1562         }
1563 #endif
1564         lli->lli_inode_magic = LLI_INODE_DEAD;
1565
1566         if (S_ISDIR(inode->i_mode))
1567                 ll_dir_clear_lsm_md(inode);
1568         else if (S_ISREG(inode->i_mode) && !is_bad_inode(inode))
1569                 LASSERT(list_empty(&lli->lli_agl_list));
1570
1571         /*
1572          * XXX This has to be done before lsm is freed below, because
1573          * cl_object still uses inode lsm.
1574          */
1575         cl_inode_fini(inode);
1576
1577         EXIT;
1578 }
1579
1580 static int ll_md_setattr(struct dentry *dentry, struct md_op_data *op_data)
1581 {
1582         struct lustre_md md;
1583         struct inode *inode = dentry->d_inode;
1584         struct ll_sb_info *sbi = ll_i2sbi(inode);
1585         struct ptlrpc_request *request = NULL;
1586         int rc, ia_valid;
1587         ENTRY;
1588
1589         op_data = ll_prep_md_op_data(op_data, inode, NULL, NULL, 0, 0,
1590                                      LUSTRE_OPC_ANY, NULL);
1591         if (IS_ERR(op_data))
1592                 RETURN(PTR_ERR(op_data));
1593
1594         rc = md_setattr(sbi->ll_md_exp, op_data, NULL, 0, &request);
1595         if (rc) {
1596                 ptlrpc_req_finished(request);
1597                 if (rc == -ENOENT) {
1598                         clear_nlink(inode);
1599                         /* Unlinked special device node? Or just a race?
1600                          * Pretend we done everything. */
1601                         if (!S_ISREG(inode->i_mode) &&
1602                             !S_ISDIR(inode->i_mode)) {
1603                                 ia_valid = op_data->op_attr.ia_valid;
1604                                 op_data->op_attr.ia_valid &= ~TIMES_SET_FLAGS;
1605                                 rc = simple_setattr(dentry, &op_data->op_attr);
1606                                 op_data->op_attr.ia_valid = ia_valid;
1607                         }
1608                 } else if (rc != -EPERM && rc != -EACCES && rc != -ETXTBSY) {
1609                         CERROR("md_setattr fails: rc = %d\n", rc);
1610                 }
1611                 RETURN(rc);
1612         }
1613
1614         rc = md_get_lustre_md(sbi->ll_md_exp, request, sbi->ll_dt_exp,
1615                               sbi->ll_md_exp, &md);
1616         if (rc) {
1617                 ptlrpc_req_finished(request);
1618                 RETURN(rc);
1619         }
1620
1621         ia_valid = op_data->op_attr.ia_valid;
1622         /* inode size will be in ll_setattr_ost, can't do it now since dirty
1623          * cache is not cleared yet. */
1624         op_data->op_attr.ia_valid &= ~(TIMES_SET_FLAGS | ATTR_SIZE);
1625         if (S_ISREG(inode->i_mode))
1626                 inode_lock(inode);
1627         rc = simple_setattr(dentry, &op_data->op_attr);
1628         if (S_ISREG(inode->i_mode))
1629                 inode_unlock(inode);
1630         op_data->op_attr.ia_valid = ia_valid;
1631
1632         rc = ll_update_inode(inode, &md);
1633         ptlrpc_req_finished(request);
1634
1635         RETURN(rc);
1636 }
1637
1638 /* If this inode has objects allocated to it (lsm != NULL), then the OST
1639  * object(s) determine the file size and mtime.  Otherwise, the MDS will
1640  * keep these values until such a time that objects are allocated for it.
1641  * We do the MDS operations first, as it is checking permissions for us.
1642  * We don't to the MDS RPC if there is nothing that we want to store there,
1643  * otherwise there is no harm in updating mtime/atime on the MDS if we are
1644  * going to do an RPC anyways.
1645  *
1646  * If we are doing a truncate, we will send the mtime and ctime updates
1647  * to the OST with the punch RPC, otherwise we do an explicit setattr RPC.
1648  * I don't believe it is possible to get e.g. ATTR_MTIME_SET and ATTR_SIZE
1649  * at the same time.
1650  *
1651  * In case of HSMimport, we only set attr on MDS.
1652  */
1653 int ll_setattr_raw(struct dentry *dentry, struct iattr *attr,
1654                    enum op_xvalid xvalid, bool hsm_import)
1655 {
1656         struct inode *inode = dentry->d_inode;
1657         struct ll_inode_info *lli = ll_i2info(inode);
1658         struct md_op_data *op_data = NULL;
1659         int rc = 0;
1660
1661         ENTRY;
1662
1663         CDEBUG(D_VFSTRACE, "%s: setattr inode "DFID"(%p) from %llu to %llu, "
1664                "valid %x, hsm_import %d\n",
1665                ll_i2sbi(inode)->ll_fsname, PFID(&lli->lli_fid),
1666                inode, i_size_read(inode), attr->ia_size, attr->ia_valid,
1667                hsm_import);
1668
1669         if (attr->ia_valid & ATTR_SIZE) {
1670                 /* Check new size against VFS/VM file size limit and rlimit */
1671                 rc = inode_newsize_ok(inode, attr->ia_size);
1672                 if (rc)
1673                         RETURN(rc);
1674
1675                 /* The maximum Lustre file size is variable, based on the
1676                  * OST maximum object size and number of stripes.  This
1677                  * needs another check in addition to the VFS check above. */
1678                 if (attr->ia_size > ll_file_maxbytes(inode)) {
1679                         CDEBUG(D_INODE,"file "DFID" too large %llu > %llu\n",
1680                                PFID(&lli->lli_fid), attr->ia_size,
1681                                ll_file_maxbytes(inode));
1682                         RETURN(-EFBIG);
1683                 }
1684
1685                 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
1686         }
1687
1688         /* POSIX: check before ATTR_*TIME_SET set (from inode_change_ok) */
1689         if (attr->ia_valid & TIMES_SET_FLAGS) {
1690                 if ((!uid_eq(current_fsuid(), inode->i_uid)) &&
1691                     !cfs_capable(CFS_CAP_FOWNER))
1692                         RETURN(-EPERM);
1693         }
1694
1695         /* We mark all of the fields "set" so MDS/OST does not re-set them */
1696         if (!(xvalid & OP_XVALID_CTIME_SET) &&
1697              (attr->ia_valid & ATTR_CTIME)) {
1698                 attr->ia_ctime = current_time(inode);
1699                 xvalid |= OP_XVALID_CTIME_SET;
1700         }
1701         if (!(attr->ia_valid & ATTR_ATIME_SET) &&
1702             (attr->ia_valid & ATTR_ATIME)) {
1703                 attr->ia_atime = current_time(inode);
1704                 attr->ia_valid |= ATTR_ATIME_SET;
1705         }
1706         if (!(attr->ia_valid & ATTR_MTIME_SET) &&
1707             (attr->ia_valid & ATTR_MTIME)) {
1708                 attr->ia_mtime = current_time(inode);
1709                 attr->ia_valid |= ATTR_MTIME_SET;
1710         }
1711
1712         if (attr->ia_valid & (ATTR_MTIME | ATTR_CTIME))
1713                 CDEBUG(D_INODE, "setting mtime %lld, ctime %lld, now = %lld\n",
1714                        (s64)attr->ia_mtime.tv_sec, (s64)attr->ia_ctime.tv_sec,
1715                        ktime_get_real_seconds());
1716
1717         if (S_ISREG(inode->i_mode)) {
1718                 if (attr->ia_valid & ATTR_SIZE)
1719                         inode_dio_write_done(inode);
1720                 inode_unlock(inode);
1721         }
1722
1723         /* We always do an MDS RPC, even if we're only changing the size;
1724          * only the MDS knows whether truncate() should fail with -ETXTBUSY */
1725
1726         OBD_ALLOC_PTR(op_data);
1727         if (op_data == NULL)
1728                 GOTO(out, rc = -ENOMEM);
1729
1730         if (!hsm_import && attr->ia_valid & ATTR_SIZE) {
1731                 /* If we are changing file size, file content is
1732                  * modified, flag it.
1733                  */
1734                 xvalid |= OP_XVALID_OWNEROVERRIDE;
1735                 op_data->op_bias |= MDS_DATA_MODIFIED;
1736                 ll_file_clear_flag(lli, LLIF_DATA_MODIFIED);
1737         }
1738
1739         if (attr->ia_valid & ATTR_FILE) {
1740                 struct ll_file_data *fd = LUSTRE_FPRIVATE(attr->ia_file);
1741
1742                 if (fd->fd_lease_och)
1743                         op_data->op_bias |= MDS_TRUNC_KEEP_LEASE;
1744         }
1745
1746         op_data->op_attr = *attr;
1747         op_data->op_xvalid = xvalid;
1748
1749         rc = ll_md_setattr(dentry, op_data);
1750         if (rc)
1751                 GOTO(out, rc);
1752
1753         if (!S_ISREG(inode->i_mode) || hsm_import)
1754                 GOTO(out, rc = 0);
1755
1756         if (attr->ia_valid & (ATTR_SIZE | ATTR_ATIME | ATTR_ATIME_SET |
1757                               ATTR_MTIME | ATTR_MTIME_SET | ATTR_CTIME) ||
1758             xvalid & OP_XVALID_CTIME_SET) {
1759                 bool cached = false;
1760
1761                 rc = pcc_inode_setattr(inode, attr, &cached);
1762                 if (cached) {
1763                         if (rc) {
1764                                 CERROR("%s: PCC inode "DFID" setattr failed: "
1765                                        "rc = %d\n",
1766                                        ll_i2sbi(inode)->ll_fsname,
1767                                        PFID(&lli->lli_fid), rc);
1768                                 GOTO(out, rc);
1769                         }
1770                 } else {
1771                         /* For truncate and utimes sending attributes to OSTs,
1772                          * setting mtime/atime to the past will be performed
1773                          * under PW [0:EOF] extent lock (new_size:EOF for
1774                          * truncate). It may seem excessive to send mtime/atime
1775                          * updates to OSTs when not setting times to past, but
1776                          * it is necessary due to possible time
1777                          * de-synchronization between MDT inode and OST objects
1778                          */
1779                         rc = cl_setattr_ost(lli->lli_clob, attr, xvalid, 0);
1780                 }
1781         }
1782
1783         /* If the file was restored, it needs to set dirty flag.
1784          *
1785          * We've already sent MDS_DATA_MODIFIED flag in
1786          * ll_md_setattr() for truncate. However, the MDT refuses to
1787          * set the HS_DIRTY flag on released files, so we have to set
1788          * it again if the file has been restored. Please check how
1789          * LLIF_DATA_MODIFIED is set in vvp_io_setattr_fini().
1790          *
1791          * Please notice that if the file is not released, the previous
1792          * MDS_DATA_MODIFIED has taken effect and usually
1793          * LLIF_DATA_MODIFIED is not set(see vvp_io_setattr_fini()).
1794          * This way we can save an RPC for common open + trunc
1795          * operation. */
1796         if (ll_file_test_and_clear_flag(lli, LLIF_DATA_MODIFIED)) {
1797                 struct hsm_state_set hss = {
1798                         .hss_valid = HSS_SETMASK,
1799                         .hss_setmask = HS_DIRTY,
1800                 };
1801                 int rc2;
1802
1803                 rc2 = ll_hsm_state_set(inode, &hss);
1804                 /* truncate and write can happen at the same time, so that
1805                  * the file can be set modified even though the file is not
1806                  * restored from released state, and ll_hsm_state_set() is
1807                  * not applicable for the file, and rc2 < 0 is normal in this
1808                  * case. */
1809                 if (rc2 < 0)
1810                         CDEBUG(D_INFO, DFID "HSM set dirty failed: rc2 = %d\n",
1811                                PFID(ll_inode2fid(inode)), rc2);
1812         }
1813
1814         EXIT;
1815 out:
1816         if (op_data != NULL)
1817                 ll_finish_md_op_data(op_data);
1818
1819         if (S_ISREG(inode->i_mode)) {
1820                 inode_lock(inode);
1821                 if ((attr->ia_valid & ATTR_SIZE) && !hsm_import)
1822                         inode_dio_wait(inode);
1823                 /* Once we've got the i_mutex, it's safe to set the S_NOSEC
1824                  * flag.  ll_update_inode (called from ll_md_setattr), clears
1825                  * inode flags, so there is a gap where S_NOSEC is not set.
1826                  * This can cause a writer to take the i_mutex unnecessarily,
1827                  * but this is safe to do and should be rare. */
1828                 inode_has_no_xattr(inode);
1829         }
1830
1831         ll_stats_ops_tally(ll_i2sbi(inode), (attr->ia_valid & ATTR_SIZE) ?
1832                         LPROC_LL_TRUNC : LPROC_LL_SETATTR, 1);
1833
1834         return rc;
1835 }
1836
1837 int ll_setattr(struct dentry *de, struct iattr *attr)
1838 {
1839         int mode = de->d_inode->i_mode;
1840         enum op_xvalid xvalid = 0;
1841
1842         if ((attr->ia_valid & (ATTR_CTIME|ATTR_SIZE|ATTR_MODE)) ==
1843                               (ATTR_CTIME|ATTR_SIZE|ATTR_MODE))
1844                 xvalid |= OP_XVALID_OWNEROVERRIDE;
1845
1846         if (((attr->ia_valid & (ATTR_MODE|ATTR_FORCE|ATTR_SIZE)) ==
1847                                (ATTR_SIZE|ATTR_MODE)) &&
1848             (((mode & S_ISUID) && !(attr->ia_mode & S_ISUID)) ||
1849              (((mode & (S_ISGID|S_IXGRP)) == (S_ISGID|S_IXGRP)) &&
1850               !(attr->ia_mode & S_ISGID))))
1851                 attr->ia_valid |= ATTR_FORCE;
1852
1853         if ((attr->ia_valid & ATTR_MODE) &&
1854             (mode & S_ISUID) &&
1855             !(attr->ia_mode & S_ISUID) &&
1856             !(attr->ia_valid & ATTR_KILL_SUID))
1857                 attr->ia_valid |= ATTR_KILL_SUID;
1858
1859         if ((attr->ia_valid & ATTR_MODE) &&
1860             ((mode & (S_ISGID|S_IXGRP)) == (S_ISGID|S_IXGRP)) &&
1861             !(attr->ia_mode & S_ISGID) &&
1862             !(attr->ia_valid & ATTR_KILL_SGID))
1863                 attr->ia_valid |= ATTR_KILL_SGID;
1864
1865         return ll_setattr_raw(de, attr, xvalid, false);
1866 }
1867
1868 int ll_statfs_internal(struct ll_sb_info *sbi, struct obd_statfs *osfs,
1869                        u32 flags)
1870 {
1871         struct obd_statfs obd_osfs = { 0 };
1872         time64_t max_age;
1873         int rc;
1874
1875         ENTRY;
1876         max_age = ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS;
1877
1878         rc = obd_statfs(NULL, sbi->ll_md_exp, osfs, max_age, flags);
1879         if (rc)
1880                 RETURN(rc);
1881
1882         osfs->os_type = LL_SUPER_MAGIC;
1883
1884         CDEBUG(D_SUPER, "MDC blocks %llu/%llu objects %llu/%llu\n",
1885               osfs->os_bavail, osfs->os_blocks, osfs->os_ffree, osfs->os_files);
1886
1887         if (osfs->os_state & OS_STATE_SUM)
1888                 GOTO(out, rc);
1889
1890         if (sbi->ll_flags & LL_SBI_LAZYSTATFS)
1891                 flags |= OBD_STATFS_NODELAY;
1892
1893         rc = obd_statfs(NULL, sbi->ll_dt_exp, &obd_osfs, max_age, flags);
1894         if (rc) /* Possibly a filesystem with no OSTs.  Report MDT totals. */
1895                 GOTO(out, rc = 0);
1896
1897         CDEBUG(D_SUPER, "OSC blocks %llu/%llu objects %llu/%llu\n",
1898                obd_osfs.os_bavail, obd_osfs.os_blocks, obd_osfs.os_ffree,
1899                obd_osfs.os_files);
1900
1901         osfs->os_bsize = obd_osfs.os_bsize;
1902         osfs->os_blocks = obd_osfs.os_blocks;
1903         osfs->os_bfree = obd_osfs.os_bfree;
1904         osfs->os_bavail = obd_osfs.os_bavail;
1905
1906         /* If we have _some_ OSTs, but don't have as many free objects on the
1907          * OSTs as inodes on the MDTs, reduce the reported number of inodes
1908          * to compensate, so that the "inodes in use" number is correct.
1909          * This should be kept in sync with lod_statfs() behaviour.
1910          */
1911         if (obd_osfs.os_files && obd_osfs.os_ffree < osfs->os_ffree) {
1912                 osfs->os_files = (osfs->os_files - osfs->os_ffree) +
1913                                  obd_osfs.os_ffree;
1914                 osfs->os_ffree = obd_osfs.os_ffree;
1915         }
1916
1917 out:
1918         RETURN(rc);
1919 }
1920 int ll_statfs(struct dentry *de, struct kstatfs *sfs)
1921 {
1922         struct super_block *sb = de->d_sb;
1923         struct obd_statfs osfs;
1924         __u64 fsid = huge_encode_dev(sb->s_dev);
1925         int rc;
1926
1927         CDEBUG(D_VFSTRACE, "VFS Op: at %llu jiffies\n", get_jiffies_64());
1928         ll_stats_ops_tally(ll_s2sbi(sb), LPROC_LL_STAFS, 1);
1929
1930         /* Some amount of caching on the client is allowed */
1931         rc = ll_statfs_internal(ll_s2sbi(sb), &osfs, OBD_STATFS_SUM);
1932         if (rc)
1933                 return rc;
1934
1935         statfs_unpack(sfs, &osfs);
1936
1937         /* We need to downshift for all 32-bit kernels, because we can't
1938          * tell if the kernel is being called via sys_statfs64() or not.
1939          * Stop before overflowing f_bsize - in which case it is better
1940          * to just risk EOVERFLOW if caller is using old sys_statfs(). */
1941         if (sizeof(long) < 8) {
1942                 while (osfs.os_blocks > ~0UL && sfs->f_bsize < 0x40000000) {
1943                         sfs->f_bsize <<= 1;
1944
1945                         osfs.os_blocks >>= 1;
1946                         osfs.os_bfree >>= 1;
1947                         osfs.os_bavail >>= 1;
1948                 }
1949         }
1950
1951         sfs->f_blocks = osfs.os_blocks;
1952         sfs->f_bfree = osfs.os_bfree;
1953         sfs->f_bavail = osfs.os_bavail;
1954         sfs->f_fsid.val[0] = (__u32)fsid;
1955         sfs->f_fsid.val[1] = (__u32)(fsid >> 32);
1956         return 0;
1957 }
1958
1959 void ll_inode_size_lock(struct inode *inode)
1960 {
1961         struct ll_inode_info *lli;
1962
1963         LASSERT(!S_ISDIR(inode->i_mode));
1964
1965         lli = ll_i2info(inode);
1966         mutex_lock(&lli->lli_size_mutex);
1967 }
1968
1969 void ll_inode_size_unlock(struct inode *inode)
1970 {
1971         struct ll_inode_info *lli;
1972
1973         lli = ll_i2info(inode);
1974         mutex_unlock(&lli->lli_size_mutex);
1975 }
1976
1977 void ll_update_inode_flags(struct inode *inode, int ext_flags)
1978 {
1979         inode->i_flags = ll_ext_to_inode_flags(ext_flags);
1980         if (ext_flags & LUSTRE_PROJINHERIT_FL)
1981                 ll_file_set_flag(ll_i2info(inode), LLIF_PROJECT_INHERIT);
1982         else
1983                 ll_file_clear_flag(ll_i2info(inode), LLIF_PROJECT_INHERIT);
1984 }
1985
1986 int ll_update_inode(struct inode *inode, struct lustre_md *md)
1987 {
1988         struct ll_inode_info *lli = ll_i2info(inode);
1989         struct mdt_body *body = md->body;
1990         struct ll_sb_info *sbi = ll_i2sbi(inode);
1991         int rc = 0;
1992
1993         if (body->mbo_valid & OBD_MD_FLEASIZE) {
1994                 rc = cl_file_inode_init(inode, md);
1995                 if (rc)
1996                         return rc;
1997         }
1998
1999         if (S_ISDIR(inode->i_mode)) {
2000                 rc = ll_update_lsm_md(inode, md);
2001                 if (rc != 0)
2002                         return rc;
2003         }
2004
2005 #ifdef CONFIG_FS_POSIX_ACL
2006         if (body->mbo_valid & OBD_MD_FLACL) {
2007                 spin_lock(&lli->lli_lock);
2008                 if (lli->lli_posix_acl)
2009                         posix_acl_release(lli->lli_posix_acl);
2010                 lli->lli_posix_acl = md->posix_acl;
2011                 spin_unlock(&lli->lli_lock);
2012         }
2013 #endif
2014         inode->i_ino = cl_fid_build_ino(&body->mbo_fid1,
2015                                         sbi->ll_flags & LL_SBI_32BIT_API);
2016         inode->i_generation = cl_fid_build_gen(&body->mbo_fid1);
2017
2018         if (body->mbo_valid & OBD_MD_FLATIME) {
2019                 if (body->mbo_atime > inode->i_atime.tv_sec)
2020                         inode->i_atime.tv_sec = body->mbo_atime;
2021                 lli->lli_atime = body->mbo_atime;
2022         }
2023
2024         if (body->mbo_valid & OBD_MD_FLMTIME) {
2025                 if (body->mbo_mtime > inode->i_mtime.tv_sec) {
2026                         CDEBUG(D_INODE,
2027                                "setting ino %lu mtime from %lld to %llu\n",
2028                                inode->i_ino, (s64)inode->i_mtime.tv_sec,
2029                                body->mbo_mtime);
2030                         inode->i_mtime.tv_sec = body->mbo_mtime;
2031                 }
2032                 lli->lli_mtime = body->mbo_mtime;
2033         }
2034
2035         if (body->mbo_valid & OBD_MD_FLCTIME) {
2036                 if (body->mbo_ctime > inode->i_ctime.tv_sec)
2037                         inode->i_ctime.tv_sec = body->mbo_ctime;
2038                 lli->lli_ctime = body->mbo_ctime;
2039         }
2040
2041         /* Clear i_flags to remove S_NOSEC before permissions are updated */
2042         if (body->mbo_valid & OBD_MD_FLFLAGS)
2043                 ll_update_inode_flags(inode, body->mbo_flags);
2044         if (body->mbo_valid & OBD_MD_FLMODE)
2045                 inode->i_mode = (inode->i_mode & S_IFMT) |
2046                                 (body->mbo_mode & ~S_IFMT);
2047
2048         if (body->mbo_valid & OBD_MD_FLTYPE)
2049                 inode->i_mode = (inode->i_mode & ~S_IFMT) |
2050                                 (body->mbo_mode & S_IFMT);
2051
2052         LASSERT(inode->i_mode != 0);
2053         if (S_ISREG(inode->i_mode))
2054                 inode->i_blkbits = min(PTLRPC_MAX_BRW_BITS + 1,
2055                                        LL_MAX_BLKSIZE_BITS);
2056         else
2057                 inode->i_blkbits = inode->i_sb->s_blocksize_bits;
2058
2059         if (body->mbo_valid & OBD_MD_FLUID)
2060                 inode->i_uid = make_kuid(&init_user_ns, body->mbo_uid);
2061         if (body->mbo_valid & OBD_MD_FLGID)
2062                 inode->i_gid = make_kgid(&init_user_ns, body->mbo_gid);
2063         if (body->mbo_valid & OBD_MD_FLPROJID)
2064                 lli->lli_projid = body->mbo_projid;
2065         if (body->mbo_valid & OBD_MD_FLNLINK)
2066                 set_nlink(inode, body->mbo_nlink);
2067         if (body->mbo_valid & OBD_MD_FLRDEV)
2068                 inode->i_rdev = old_decode_dev(body->mbo_rdev);
2069
2070         if (body->mbo_valid & OBD_MD_FLID) {
2071                 /* FID shouldn't be changed! */
2072                 if (fid_is_sane(&lli->lli_fid)) {
2073                         LASSERTF(lu_fid_eq(&lli->lli_fid, &body->mbo_fid1),
2074                                  "Trying to change FID "DFID
2075                                  " to the "DFID", inode "DFID"(%p)\n",
2076                                  PFID(&lli->lli_fid), PFID(&body->mbo_fid1),
2077                                  PFID(ll_inode2fid(inode)), inode);
2078                 } else {
2079                         lli->lli_fid = body->mbo_fid1;
2080                 }
2081         }
2082
2083         LASSERT(fid_seq(&lli->lli_fid) != 0);
2084
2085         if (body->mbo_valid & OBD_MD_FLSIZE) {
2086                 i_size_write(inode, body->mbo_size);
2087
2088                 CDEBUG(D_VFSTRACE, "inode="DFID", updating i_size %llu\n",
2089                        PFID(ll_inode2fid(inode)),
2090                        (unsigned long long)body->mbo_size);
2091
2092                 if (body->mbo_valid & OBD_MD_FLBLOCKS)
2093                         inode->i_blocks = body->mbo_blocks;
2094         }
2095
2096         if (body->mbo_valid & OBD_MD_TSTATE) {
2097                 /* Set LLIF_FILE_RESTORING if restore ongoing and
2098                  * clear it when done to ensure to start again
2099                  * glimpsing updated attrs
2100                  */
2101                 if (body->mbo_t_state & MS_RESTORE)
2102                         ll_file_set_flag(lli, LLIF_FILE_RESTORING);
2103                 else
2104                         ll_file_clear_flag(lli, LLIF_FILE_RESTORING);
2105         }
2106
2107         return 0;
2108 }
2109
2110 int ll_read_inode2(struct inode *inode, void *opaque)
2111 {
2112         struct lustre_md *md = opaque;
2113         struct ll_inode_info *lli = ll_i2info(inode);
2114         int     rc;
2115         ENTRY;
2116
2117         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
2118                PFID(&lli->lli_fid), inode);
2119
2120         /* Core attributes from the MDS first.  This is a new inode, and
2121          * the VFS doesn't zero times in the core inode so we have to do
2122          * it ourselves.  They will be overwritten by either MDS or OST
2123          * attributes - we just need to make sure they aren't newer.
2124          */
2125         inode->i_mtime.tv_sec = 0;
2126         inode->i_atime.tv_sec = 0;
2127         inode->i_ctime.tv_sec = 0;
2128         inode->i_rdev = 0;
2129         rc = ll_update_inode(inode, md);
2130         if (rc != 0)
2131                 RETURN(rc);
2132
2133         /* OIDEBUG(inode); */
2134
2135 #ifdef HAVE_BACKING_DEV_INFO
2136         /* initializing backing dev info. */
2137         inode->i_mapping->backing_dev_info = &s2lsi(inode->i_sb)->lsi_bdi;
2138 #endif
2139         if (S_ISREG(inode->i_mode)) {
2140                 struct ll_sb_info *sbi = ll_i2sbi(inode);
2141                 inode->i_op = &ll_file_inode_operations;
2142                 inode->i_fop = sbi->ll_fop;
2143                 inode->i_mapping->a_ops = (struct address_space_operations *)&ll_aops;
2144                 EXIT;
2145         } else if (S_ISDIR(inode->i_mode)) {
2146                 inode->i_op = &ll_dir_inode_operations;
2147                 inode->i_fop = &ll_dir_operations;
2148                 EXIT;
2149         } else if (S_ISLNK(inode->i_mode)) {
2150                 inode->i_op = &ll_fast_symlink_inode_operations;
2151                 EXIT;
2152         } else {
2153                 inode->i_op = &ll_special_inode_operations;
2154
2155                 init_special_inode(inode, inode->i_mode,
2156                                    inode->i_rdev);
2157
2158                 EXIT;
2159         }
2160
2161         return 0;
2162 }
2163
2164 void ll_delete_inode(struct inode *inode)
2165 {
2166         struct ll_inode_info *lli = ll_i2info(inode);
2167         struct address_space *mapping = &inode->i_data;
2168         unsigned long nrpages;
2169         ENTRY;
2170
2171         if (S_ISREG(inode->i_mode) && lli->lli_clob != NULL) {
2172                 /* It is last chance to write out dirty pages,
2173                  * otherwise we may lose data while umount.
2174                  *
2175                  * If i_nlink is 0 then just discard data. This is safe because
2176                  * local inode gets i_nlink 0 from server only for the last
2177                  * unlink, so that file is not opened somewhere else
2178                  */
2179                 cl_sync_file_range(inode, 0, OBD_OBJECT_EOF, inode->i_nlink ?
2180                                    CL_FSYNC_LOCAL : CL_FSYNC_DISCARD, 1);
2181         }
2182         truncate_inode_pages_final(mapping);
2183
2184         /* Workaround for LU-118: Note nrpages may not be totally updated when
2185          * truncate_inode_pages() returns, as there can be a page in the process
2186          * of deletion (inside __delete_from_page_cache()) in the specified
2187          * range. Thus mapping->nrpages can be non-zero when this function
2188          * returns even after truncation of the whole mapping.  Only do this if
2189          * npages isn't already zero.
2190          */
2191         nrpages = mapping->nrpages;
2192         if (nrpages) {
2193                 xa_lock_irq(&mapping->i_pages);
2194                 nrpages = mapping->nrpages;
2195                 xa_unlock_irq(&mapping->i_pages);
2196         } /* Workaround end */
2197
2198         LASSERTF(nrpages == 0, "%s: inode="DFID"(%p) nrpages=%lu, "
2199                  "see https://jira.whamcloud.com/browse/LU-118\n",
2200                  ll_i2sbi(inode)->ll_fsname,
2201                  PFID(ll_inode2fid(inode)), inode, nrpages);
2202
2203 #ifdef HAVE_SBOPS_EVICT_INODE
2204         ll_clear_inode(inode);
2205 #endif
2206         clear_inode(inode);
2207
2208         EXIT;
2209 }
2210
2211 int ll_iocontrol(struct inode *inode, struct file *file,
2212                  unsigned int cmd, unsigned long arg)
2213 {
2214         struct ll_sb_info *sbi = ll_i2sbi(inode);
2215         struct ptlrpc_request *req = NULL;
2216         int rc, flags = 0;
2217         ENTRY;
2218
2219         switch (cmd) {
2220         case FS_IOC_GETFLAGS: {
2221                 struct mdt_body *body;
2222                 struct md_op_data *op_data;
2223
2224                 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL,
2225                                              0, 0, LUSTRE_OPC_ANY,
2226                                              NULL);
2227                 if (IS_ERR(op_data))
2228                         RETURN(PTR_ERR(op_data));
2229
2230                 op_data->op_valid = OBD_MD_FLFLAGS;
2231                 rc = md_getattr(sbi->ll_md_exp, op_data, &req);
2232                 ll_finish_md_op_data(op_data);
2233                 if (rc) {
2234                         CERROR("%s: failure inode "DFID": rc = %d\n",
2235                                sbi->ll_md_exp->exp_obd->obd_name,
2236                                PFID(ll_inode2fid(inode)), rc);
2237                         RETURN(-abs(rc));
2238                 }
2239
2240                 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
2241
2242                 flags = body->mbo_flags;
2243
2244                 ptlrpc_req_finished(req);
2245
2246                 RETURN(put_user(flags, (int __user *)arg));
2247         }
2248         case FS_IOC_SETFLAGS: {
2249                 struct iattr *attr;
2250                 struct md_op_data *op_data;
2251                 struct cl_object *obj;
2252                 struct fsxattr fa = { 0 };
2253
2254                 if (get_user(flags, (int __user *)arg))
2255                         RETURN(-EFAULT);
2256
2257                 fa.fsx_projid = ll_i2info(inode)->lli_projid;
2258                 if (flags & LUSTRE_PROJINHERIT_FL)
2259                         fa.fsx_xflags = FS_XFLAG_PROJINHERIT;
2260
2261                 rc = ll_ioctl_check_project(inode, &fa);
2262                 if (rc)
2263                         RETURN(rc);
2264
2265                 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
2266                                              LUSTRE_OPC_ANY, NULL);
2267                 if (IS_ERR(op_data))
2268                         RETURN(PTR_ERR(op_data));
2269
2270                 op_data->op_attr_flags = flags;
2271                 op_data->op_xvalid |= OP_XVALID_FLAGS;
2272                 rc = md_setattr(sbi->ll_md_exp, op_data, NULL, 0, &req);
2273                 ll_finish_md_op_data(op_data);
2274                 ptlrpc_req_finished(req);
2275                 if (rc)
2276                         RETURN(rc);
2277
2278                 ll_update_inode_flags(inode, flags);
2279
2280                 obj = ll_i2info(inode)->lli_clob;
2281                 if (obj == NULL)
2282                         RETURN(0);
2283
2284                 OBD_ALLOC_PTR(attr);
2285                 if (attr == NULL)
2286                         RETURN(-ENOMEM);
2287
2288                 rc = cl_setattr_ost(obj, attr, OP_XVALID_FLAGS, flags);
2289
2290                 OBD_FREE_PTR(attr);
2291                 RETURN(rc);
2292         }
2293         default:
2294                 RETURN(-ENOSYS);
2295         }
2296
2297         RETURN(0);
2298 }
2299
2300 int ll_flush_ctx(struct inode *inode)
2301 {
2302         struct ll_sb_info  *sbi = ll_i2sbi(inode);
2303
2304         CDEBUG(D_SEC, "flush context for user %d\n",
2305                from_kuid(&init_user_ns, current_uid()));
2306
2307         obd_set_info_async(NULL, sbi->ll_md_exp,
2308                            sizeof(KEY_FLUSH_CTX), KEY_FLUSH_CTX,
2309                            0, NULL, NULL);
2310         obd_set_info_async(NULL, sbi->ll_dt_exp,
2311                            sizeof(KEY_FLUSH_CTX), KEY_FLUSH_CTX,
2312                            0, NULL, NULL);
2313         return 0;
2314 }
2315
2316 /* umount -f client means force down, don't save state */
2317 void ll_umount_begin(struct super_block *sb)
2318 {
2319         struct ll_sb_info *sbi = ll_s2sbi(sb);
2320         struct obd_device *obd;
2321         struct obd_ioctl_data *ioc_data;
2322         struct l_wait_info lwi;
2323         wait_queue_head_t waitq;
2324         ENTRY;
2325
2326         CDEBUG(D_VFSTRACE, "VFS Op: superblock %p count %d active %d\n", sb,
2327                sb->s_count, atomic_read(&sb->s_active));
2328
2329         obd = class_exp2obd(sbi->ll_md_exp);
2330         if (obd == NULL) {
2331                 CERROR("Invalid MDC connection handle %#llx\n",
2332                        sbi->ll_md_exp->exp_handle.h_cookie);
2333                 EXIT;
2334                 return;
2335         }
2336         obd->obd_force = 1;
2337
2338         obd = class_exp2obd(sbi->ll_dt_exp);
2339         if (obd == NULL) {
2340                 CERROR("Invalid LOV connection handle %#llx\n",
2341                        sbi->ll_dt_exp->exp_handle.h_cookie);
2342                 EXIT;
2343                 return;
2344         }
2345         obd->obd_force = 1;
2346
2347         OBD_ALLOC_PTR(ioc_data);
2348         if (ioc_data) {
2349                 obd_iocontrol(IOC_OSC_SET_ACTIVE, sbi->ll_md_exp,
2350                               sizeof *ioc_data, ioc_data, NULL);
2351
2352                 obd_iocontrol(IOC_OSC_SET_ACTIVE, sbi->ll_dt_exp,
2353                               sizeof *ioc_data, ioc_data, NULL);
2354
2355                 OBD_FREE_PTR(ioc_data);
2356         }
2357
2358         /* Really, we'd like to wait until there are no requests outstanding,
2359          * and then continue.  For now, we just periodically checking for vfs
2360          * to decrement mnt_cnt and hope to finish it within 10sec.
2361          */
2362         init_waitqueue_head(&waitq);
2363         lwi = LWI_TIMEOUT_INTERVAL(cfs_time_seconds(10),
2364                                    cfs_time_seconds(1), NULL, NULL);
2365         l_wait_event(waitq, may_umount(sbi->ll_mnt.mnt), &lwi);
2366
2367         EXIT;
2368 }
2369
2370 int ll_remount_fs(struct super_block *sb, int *flags, char *data)
2371 {
2372         struct ll_sb_info *sbi = ll_s2sbi(sb);
2373         char *profilenm = get_profile_name(sb);
2374         int err;
2375         __u32 read_only;
2376
2377         if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY)) {
2378                 read_only = *flags & MS_RDONLY;
2379                 err = obd_set_info_async(NULL, sbi->ll_md_exp,
2380                                          sizeof(KEY_READ_ONLY),
2381                                          KEY_READ_ONLY, sizeof(read_only),
2382                                          &read_only, NULL);
2383                 if (err) {
2384                         LCONSOLE_WARN("Failed to remount %s %s (%d)\n",
2385                                       profilenm, read_only ?
2386                                       "read-only" : "read-write", err);
2387                         return err;
2388                 }
2389
2390                 if (read_only)
2391                         sb->s_flags |= MS_RDONLY;
2392                 else
2393                         sb->s_flags &= ~MS_RDONLY;
2394
2395                 if (sbi->ll_flags & LL_SBI_VERBOSE)
2396                         LCONSOLE_WARN("Remounted %s %s\n", profilenm,
2397                                       read_only ?  "read-only" : "read-write");
2398         }
2399         return 0;
2400 }
2401
2402 /**
2403  * Cleanup the open handle that is cached on MDT-side.
2404  *
2405  * For open case, the client side open handling thread may hit error
2406  * after the MDT grant the open. Under such case, the client should
2407  * send close RPC to the MDT as cleanup; otherwise, the open handle
2408  * on the MDT will be leaked there until the client umount or evicted.
2409  *
2410  * In further, if someone unlinked the file, because the open handle
2411  * holds the reference on such file/object, then it will block the
2412  * subsequent threads that want to locate such object via FID.
2413  *
2414  * \param[in] sb        super block for this file-system
2415  * \param[in] open_req  pointer to the original open request
2416  */
2417 void ll_open_cleanup(struct super_block *sb, struct ptlrpc_request *open_req)
2418 {
2419         struct mdt_body                 *body;
2420         struct md_op_data               *op_data;
2421         struct ptlrpc_request           *close_req = NULL;
2422         struct obd_export               *exp       = ll_s2sbi(sb)->ll_md_exp;
2423         ENTRY;
2424
2425         body = req_capsule_server_get(&open_req->rq_pill, &RMF_MDT_BODY);
2426         OBD_ALLOC_PTR(op_data);
2427         if (op_data == NULL) {
2428                 CWARN("%s: cannot allocate op_data to release open handle for "
2429                       DFID"\n", ll_s2sbi(sb)->ll_fsname, PFID(&body->mbo_fid1));
2430
2431                 RETURN_EXIT;
2432         }
2433
2434         op_data->op_fid1 = body->mbo_fid1;
2435         op_data->op_open_handle = body->mbo_open_handle;
2436         op_data->op_mod_time = ktime_get_real_seconds();
2437         md_close(exp, op_data, NULL, &close_req);
2438         ptlrpc_req_finished(close_req);
2439         ll_finish_md_op_data(op_data);
2440
2441         EXIT;
2442 }
2443
2444 int ll_prep_inode(struct inode **inode, struct ptlrpc_request *req,
2445                   struct super_block *sb, struct lookup_intent *it)
2446 {
2447         struct ll_sb_info *sbi = NULL;
2448         struct lustre_md md = { NULL };
2449         int rc;
2450         ENTRY;
2451
2452         LASSERT(*inode || sb);
2453         sbi = sb ? ll_s2sbi(sb) : ll_i2sbi(*inode);
2454         rc = md_get_lustre_md(sbi->ll_md_exp, req, sbi->ll_dt_exp,
2455                               sbi->ll_md_exp, &md);
2456         if (rc != 0)
2457                 GOTO(out, rc);
2458
2459         if (*inode) {
2460                 rc = ll_update_inode(*inode, &md);
2461                 if (rc != 0)
2462                         GOTO(out, rc);
2463         } else {
2464                 LASSERT(sb != NULL);
2465
2466                 /*
2467                  * At this point server returns to client's same fid as client
2468                  * generated for creating. So using ->fid1 is okay here.
2469                  */
2470                 if (!fid_is_sane(&md.body->mbo_fid1)) {
2471                         CERROR("%s: Fid is insane "DFID"\n",
2472                                 sbi->ll_fsname,
2473                                 PFID(&md.body->mbo_fid1));
2474                         GOTO(out, rc = -EINVAL);
2475                 }
2476
2477                 *inode = ll_iget(sb, cl_fid_build_ino(&md.body->mbo_fid1,
2478                                              sbi->ll_flags & LL_SBI_32BIT_API),
2479                                  &md);
2480                 if (IS_ERR(*inode)) {
2481 #ifdef CONFIG_FS_POSIX_ACL
2482                         if (md.posix_acl) {
2483                                 posix_acl_release(md.posix_acl);
2484                                 md.posix_acl = NULL;
2485                         }
2486 #endif
2487                         rc = IS_ERR(*inode) ? PTR_ERR(*inode) : -ENOMEM;
2488                         *inode = NULL;
2489                         CERROR("new_inode -fatal: rc %d\n", rc);
2490                         GOTO(out, rc);
2491                 }
2492         }
2493
2494         /* Handling piggyback layout lock.
2495          * Layout lock can be piggybacked by getattr and open request.
2496          * The lsm can be applied to inode only if it comes with a layout lock
2497          * otherwise correct layout may be overwritten, for example:
2498          * 1. proc1: mdt returns a lsm but not granting layout
2499          * 2. layout was changed by another client
2500          * 3. proc2: refresh layout and layout lock granted
2501          * 4. proc1: to apply a stale layout */
2502         if (it != NULL && it->it_lock_mode != 0) {
2503                 struct lustre_handle lockh;
2504                 struct ldlm_lock *lock;
2505
2506                 lockh.cookie = it->it_lock_handle;
2507                 lock = ldlm_handle2lock(&lockh);
2508                 LASSERT(lock != NULL);
2509                 if (ldlm_has_layout(lock)) {
2510                         struct cl_object_conf conf;
2511
2512                         memset(&conf, 0, sizeof(conf));
2513                         conf.coc_opc = OBJECT_CONF_SET;
2514                         conf.coc_inode = *inode;
2515                         conf.coc_lock = lock;
2516                         conf.u.coc_layout = md.layout;
2517                         (void)ll_layout_conf(*inode, &conf);
2518                 }
2519                 LDLM_LOCK_PUT(lock);
2520         }
2521
2522         GOTO(out, rc = 0);
2523
2524 out:
2525         /* cleanup will be done if necessary */
2526         md_free_lustre_md(sbi->ll_md_exp, &md);
2527
2528         if (rc != 0 && it != NULL && it->it_op & IT_OPEN)
2529                 ll_open_cleanup(sb != NULL ? sb : (*inode)->i_sb, req);
2530
2531         return rc;
2532 }
2533
2534 int ll_obd_statfs(struct inode *inode, void __user *arg)
2535 {
2536         struct ll_sb_info *sbi = NULL;
2537         struct obd_export *exp;
2538         char *buf = NULL;
2539         struct obd_ioctl_data *data = NULL;
2540         __u32 type;
2541         int len = 0, rc;
2542
2543         if (!inode || !(sbi = ll_i2sbi(inode)))
2544                 GOTO(out_statfs, rc = -EINVAL);
2545
2546         rc = obd_ioctl_getdata(&buf, &len, arg);
2547         if (rc)
2548                 GOTO(out_statfs, rc);
2549
2550         data = (void*)buf;
2551         if (!data->ioc_inlbuf1 || !data->ioc_inlbuf2 ||
2552             !data->ioc_pbuf1 || !data->ioc_pbuf2)
2553                 GOTO(out_statfs, rc = -EINVAL);
2554
2555         if (data->ioc_inllen1 != sizeof(__u32) ||
2556             data->ioc_inllen2 != sizeof(__u32) ||
2557             data->ioc_plen1 != sizeof(struct obd_statfs) ||
2558             data->ioc_plen2 != sizeof(struct obd_uuid))
2559                 GOTO(out_statfs, rc = -EINVAL);
2560
2561         memcpy(&type, data->ioc_inlbuf1, sizeof(__u32));
2562         if (type & LL_STATFS_LMV)
2563                 exp = sbi->ll_md_exp;
2564         else if (type & LL_STATFS_LOV)
2565                 exp = sbi->ll_dt_exp;
2566         else
2567                 GOTO(out_statfs, rc = -ENODEV);
2568
2569         rc = obd_iocontrol(IOC_OBD_STATFS, exp, len, buf, NULL);
2570         if (rc)
2571                 GOTO(out_statfs, rc);
2572 out_statfs:
2573         OBD_FREE_LARGE(buf, len);
2574         return rc;
2575 }
2576
2577 /*
2578  * this is normally called in ll_fini_md_op_data(), but sometimes it needs to
2579  * be called early to avoid deadlock.
2580  */
2581 void ll_unlock_md_op_lsm(struct md_op_data *op_data)
2582 {
2583         if (op_data->op_mea2_sem) {
2584                 up_read(op_data->op_mea2_sem);
2585                 op_data->op_mea2_sem = NULL;
2586         }
2587
2588         if (op_data->op_mea1_sem) {
2589                 up_read(op_data->op_mea1_sem);
2590                 op_data->op_mea1_sem = NULL;
2591         }
2592 }
2593
2594 /* this function prepares md_op_data hint for passing it down to MD stack. */
2595 struct md_op_data *ll_prep_md_op_data(struct md_op_data *op_data,
2596                                       struct inode *i1, struct inode *i2,
2597                                       const char *name, size_t namelen,
2598                                       __u32 mode, __u32 opc, void *data)
2599 {
2600         LASSERT(i1 != NULL);
2601
2602         if (name == NULL) {
2603                 /* Do not reuse namelen for something else. */
2604                 if (namelen != 0)
2605                         return ERR_PTR(-EINVAL);
2606         } else {
2607                 if (namelen > ll_i2sbi(i1)->ll_namelen)
2608                         return ERR_PTR(-ENAMETOOLONG);
2609
2610                 if (!lu_name_is_valid_2(name, namelen))
2611                         return ERR_PTR(-EINVAL);
2612         }
2613
2614         if (op_data == NULL)
2615                 OBD_ALLOC_PTR(op_data);
2616
2617         if (op_data == NULL)
2618                 return ERR_PTR(-ENOMEM);
2619
2620         ll_i2gids(op_data->op_suppgids, i1, i2);
2621         op_data->op_fid1 = *ll_inode2fid(i1);
2622         op_data->op_default_stripe_offset = -1;
2623
2624         if (S_ISDIR(i1->i_mode)) {
2625                 down_read(&ll_i2info(i1)->lli_lsm_sem);
2626                 op_data->op_mea1_sem = &ll_i2info(i1)->lli_lsm_sem;
2627                 op_data->op_mea1 = ll_i2info(i1)->lli_lsm_md;
2628                 if (opc == LUSTRE_OPC_MKDIR)
2629                         op_data->op_default_stripe_offset =
2630                                    ll_i2info(i1)->lli_def_stripe_offset;
2631         }
2632
2633         if (i2) {
2634                 op_data->op_fid2 = *ll_inode2fid(i2);
2635                 if (S_ISDIR(i2->i_mode)) {
2636                         if (i2 != i1) {
2637                                 down_read(&ll_i2info(i2)->lli_lsm_sem);
2638                                 op_data->op_mea2_sem =
2639                                                 &ll_i2info(i2)->lli_lsm_sem;
2640                         }
2641                         op_data->op_mea2 = ll_i2info(i2)->lli_lsm_md;
2642                 }
2643         } else {
2644                 fid_zero(&op_data->op_fid2);
2645         }
2646
2647         if (ll_i2sbi(i1)->ll_flags & LL_SBI_64BIT_HASH)
2648                 op_data->op_cli_flags |= CLI_HASH64;
2649
2650         if (ll_need_32bit_api(ll_i2sbi(i1)))
2651                 op_data->op_cli_flags |= CLI_API32;
2652
2653         op_data->op_name = name;
2654         op_data->op_namelen = namelen;
2655         op_data->op_mode = mode;
2656         op_data->op_mod_time = ktime_get_real_seconds();
2657         op_data->op_fsuid = from_kuid(&init_user_ns, current_fsuid());
2658         op_data->op_fsgid = from_kgid(&init_user_ns, current_fsgid());
2659         op_data->op_cap = cfs_curproc_cap_pack();
2660         op_data->op_mds = 0;
2661         if ((opc == LUSTRE_OPC_CREATE) && (name != NULL) &&
2662              filename_is_volatile(name, namelen, &op_data->op_mds)) {
2663                 op_data->op_bias |= MDS_CREATE_VOLATILE;
2664         }
2665         op_data->op_data = data;
2666
2667         return op_data;
2668 }
2669
2670 void ll_finish_md_op_data(struct md_op_data *op_data)
2671 {
2672         ll_unlock_md_op_lsm(op_data);
2673         security_release_secctx(op_data->op_file_secctx,
2674                                 op_data->op_file_secctx_size);
2675         OBD_FREE_PTR(op_data);
2676 }
2677
2678 #ifdef HAVE_SUPEROPS_USE_DENTRY
2679 int ll_show_options(struct seq_file *seq, struct dentry *dentry)
2680 #else
2681 int ll_show_options(struct seq_file *seq, struct vfsmount *vfs)
2682 #endif
2683 {
2684         struct ll_sb_info *sbi;
2685
2686 #ifdef HAVE_SUPEROPS_USE_DENTRY
2687         LASSERT((seq != NULL) && (dentry != NULL));
2688         sbi = ll_s2sbi(dentry->d_sb);
2689 #else
2690         LASSERT((seq != NULL) && (vfs != NULL));
2691         sbi = ll_s2sbi(vfs->mnt_sb);
2692 #endif
2693
2694         if (sbi->ll_flags & LL_SBI_NOLCK)
2695                 seq_puts(seq, ",nolock");
2696
2697         if (sbi->ll_flags & LL_SBI_FLOCK)
2698                 seq_puts(seq, ",flock");
2699
2700         if (sbi->ll_flags & LL_SBI_LOCALFLOCK)
2701                 seq_puts(seq, ",localflock");
2702
2703         if (sbi->ll_flags & LL_SBI_USER_XATTR)
2704                 seq_puts(seq, ",user_xattr");
2705
2706         if (sbi->ll_flags & LL_SBI_LAZYSTATFS)
2707                 seq_puts(seq, ",lazystatfs");
2708
2709         if (sbi->ll_flags & LL_SBI_USER_FID2PATH)
2710                 seq_puts(seq, ",user_fid2path");
2711
2712         if (sbi->ll_flags & LL_SBI_ALWAYS_PING)
2713                 seq_puts(seq, ",always_ping");
2714
2715         RETURN(0);
2716 }
2717
2718 /**
2719  * Get obd name by cmd, and copy out to user space
2720  */
2721 int ll_get_obd_name(struct inode *inode, unsigned int cmd, unsigned long arg)
2722 {
2723         struct ll_sb_info *sbi = ll_i2sbi(inode);
2724         struct obd_device *obd;
2725         ENTRY;
2726
2727         if (cmd == OBD_IOC_GETDTNAME)
2728                 obd = class_exp2obd(sbi->ll_dt_exp);
2729         else if (cmd == OBD_IOC_GETMDNAME)
2730                 obd = class_exp2obd(sbi->ll_md_exp);
2731         else
2732                 RETURN(-EINVAL);
2733
2734         if (!obd)
2735                 RETURN(-ENOENT);
2736
2737         if (copy_to_user((void __user *)arg, obd->obd_name,
2738                          strlen(obd->obd_name) + 1))
2739                 RETURN(-EFAULT);
2740
2741         RETURN(0);
2742 }
2743
2744 static char* ll_d_path(struct dentry *dentry, char *buf, int bufsize)
2745 {
2746         char *path = NULL;
2747
2748         struct path p;
2749
2750         p.dentry = dentry;
2751         p.mnt = current->fs->root.mnt;
2752         path_get(&p);
2753         path = d_path(&p, buf, bufsize);
2754         path_put(&p);
2755         return path;
2756 }
2757
2758 void ll_dirty_page_discard_warn(struct page *page, int ioret)
2759 {
2760         char *buf, *path = NULL;
2761         struct dentry *dentry = NULL;
2762         struct inode *inode = page->mapping->host;
2763
2764         /* this can be called inside spin lock so use GFP_ATOMIC. */
2765         buf = (char *)__get_free_page(GFP_ATOMIC);
2766         if (buf != NULL) {
2767                 dentry = d_find_alias(page->mapping->host);
2768                 if (dentry != NULL)
2769                         path = ll_d_path(dentry, buf, PAGE_SIZE);
2770         }
2771
2772         CDEBUG(D_WARNING,
2773                "%s: dirty page discard: %s/fid: "DFID"/%s may get corrupted "
2774                "(rc %d)\n", ll_i2sbi(inode)->ll_fsname,
2775                s2lsi(page->mapping->host->i_sb)->lsi_lmd->lmd_dev,
2776                PFID(ll_inode2fid(inode)),
2777                (path && !IS_ERR(path)) ? path : "", ioret);
2778
2779         if (dentry != NULL)
2780                 dput(dentry);
2781
2782         if (buf != NULL)
2783                 free_page((unsigned long)buf);
2784 }
2785
2786 ssize_t ll_copy_user_md(const struct lov_user_md __user *md,
2787                         struct lov_user_md **kbuf)
2788 {
2789         struct lov_user_md      lum;
2790         ssize_t                 lum_size;
2791         ENTRY;
2792
2793         if (copy_from_user(&lum, md, sizeof(lum)))
2794                 RETURN(-EFAULT);
2795
2796         lum_size = ll_lov_user_md_size(&lum);
2797         if (lum_size < 0)
2798                 RETURN(lum_size);
2799
2800         OBD_ALLOC(*kbuf, lum_size);
2801         if (*kbuf == NULL)
2802                 RETURN(-ENOMEM);
2803
2804         if (copy_from_user(*kbuf, md, lum_size) != 0) {
2805                 OBD_FREE(*kbuf, lum_size);
2806                 RETURN(-EFAULT);
2807         }
2808
2809         RETURN(lum_size);
2810 }
2811
2812 /*
2813  * Compute llite root squash state after a change of root squash
2814  * configuration setting or add/remove of a lnet nid
2815  */
2816 void ll_compute_rootsquash_state(struct ll_sb_info *sbi)
2817 {
2818         struct root_squash_info *squash = &sbi->ll_squash;
2819         int i;
2820         bool matched;
2821         struct lnet_process_id id;
2822
2823         /* Update norootsquash flag */
2824         down_write(&squash->rsi_sem);
2825         if (list_empty(&squash->rsi_nosquash_nids))
2826                 sbi->ll_flags &= ~LL_SBI_NOROOTSQUASH;
2827         else {
2828                 /* Do not apply root squash as soon as one of our NIDs is
2829                  * in the nosquash_nids list */
2830                 matched = false;
2831                 i = 0;
2832                 while (LNetGetId(i++, &id) != -ENOENT) {
2833                         if (LNET_NETTYP(LNET_NIDNET(id.nid)) == LOLND)
2834                                 continue;
2835                         if (cfs_match_nid(id.nid, &squash->rsi_nosquash_nids)) {
2836                                 matched = true;
2837                                 break;
2838                         }
2839                 }
2840                 if (matched)
2841                         sbi->ll_flags |= LL_SBI_NOROOTSQUASH;
2842                 else
2843                         sbi->ll_flags &= ~LL_SBI_NOROOTSQUASH;
2844         }
2845         up_write(&squash->rsi_sem);
2846 }
2847
2848 /**
2849  * Parse linkea content to extract information about a given hardlink
2850  *
2851  * \param[in]   ldata      - Initialized linkea data
2852  * \param[in]   linkno     - Link identifier
2853  * \param[out]  parent_fid - The entry's parent FID
2854  * \param[out]  ln         - Entry name destination buffer
2855  *
2856  * \retval 0 on success
2857  * \retval Appropriate negative error code on failure
2858  */
2859 static int ll_linkea_decode(struct linkea_data *ldata, unsigned int linkno,
2860                             struct lu_fid *parent_fid, struct lu_name *ln)
2861 {
2862         unsigned int    idx;
2863         int             rc;
2864         ENTRY;
2865
2866         rc = linkea_init_with_rec(ldata);
2867         if (rc < 0)
2868                 RETURN(rc);
2869
2870         if (linkno >= ldata->ld_leh->leh_reccount)
2871                 /* beyond last link */
2872                 RETURN(-ENODATA);
2873
2874         linkea_first_entry(ldata);
2875         for (idx = 0; ldata->ld_lee != NULL; idx++) {
2876                 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen, ln,
2877                                     parent_fid);
2878                 if (idx == linkno)
2879                         break;
2880
2881                 linkea_next_entry(ldata);
2882         }
2883
2884         if (idx < linkno)
2885                 RETURN(-ENODATA);
2886
2887         RETURN(0);
2888 }
2889
2890 /**
2891  * Get parent FID and name of an identified link. Operation is performed for
2892  * a given link number, letting the caller iterate over linkno to list one or
2893  * all links of an entry.
2894  *
2895  * \param[in]     file - File descriptor against which to perform the operation
2896  * \param[in,out] arg  - User-filled structure containing the linkno to operate
2897  *                       on and the available size. It is eventually filled with
2898  *                       the requested information or left untouched on error
2899  *
2900  * \retval - 0 on success
2901  * \retval - Appropriate negative error code on failure
2902  */
2903 int ll_getparent(struct file *file, struct getparent __user *arg)
2904 {
2905         struct inode            *inode = file_inode(file);
2906         struct linkea_data      *ldata;
2907         struct lu_buf            buf = LU_BUF_NULL;
2908         struct lu_name           ln;
2909         struct lu_fid            parent_fid;
2910         __u32                    linkno;
2911         __u32                    name_size;
2912         int                      rc;
2913
2914         ENTRY;
2915
2916         if (!cfs_capable(CFS_CAP_DAC_READ_SEARCH) &&
2917             !(ll_i2sbi(inode)->ll_flags & LL_SBI_USER_FID2PATH))
2918                 RETURN(-EPERM);
2919
2920         if (get_user(name_size, &arg->gp_name_size))
2921                 RETURN(-EFAULT);
2922
2923         if (get_user(linkno, &arg->gp_linkno))
2924                 RETURN(-EFAULT);
2925
2926         if (name_size > PATH_MAX)
2927                 RETURN(-EINVAL);
2928
2929         OBD_ALLOC(ldata, sizeof(*ldata));
2930         if (ldata == NULL)
2931                 RETURN(-ENOMEM);
2932
2933         rc = linkea_data_new(ldata, &buf);
2934         if (rc < 0)
2935                 GOTO(ldata_free, rc);
2936
2937 #ifdef HAVE_XATTR_HANDLER_FLAGS
2938         rc = ll_xattr_list(inode, XATTR_NAME_LINK, XATTR_TRUSTED_T, buf.lb_buf,
2939                            buf.lb_len, OBD_MD_FLXATTR);
2940 #else
2941         rc = ll_getxattr(file_dentry(file), XATTR_NAME_LINK, buf.lb_buf,
2942                          buf.lb_len);
2943 #endif /* HAVE_XATTR_HANDLER_FLAGS */
2944         if (rc < 0)
2945                 GOTO(lb_free, rc);
2946
2947         rc = ll_linkea_decode(ldata, linkno, &parent_fid, &ln);
2948         if (rc < 0)
2949                 GOTO(lb_free, rc);
2950
2951         if (ln.ln_namelen >= name_size)
2952                 GOTO(lb_free, rc = -EOVERFLOW);
2953
2954         if (copy_to_user(&arg->gp_fid, &parent_fid, sizeof(arg->gp_fid)))
2955                 GOTO(lb_free, rc = -EFAULT);
2956
2957         if (copy_to_user(&arg->gp_name, ln.ln_name, ln.ln_namelen))
2958                 GOTO(lb_free, rc = -EFAULT);
2959
2960         if (put_user('\0', arg->gp_name + ln.ln_namelen))
2961                 GOTO(lb_free, rc = -EFAULT);
2962
2963 lb_free:
2964         lu_buf_free(&buf);
2965 ldata_free:
2966         OBD_FREE(ldata, sizeof(*ldata));
2967
2968         RETURN(rc);
2969 }