Whamcloud - gitweb
LU-10092 llite: Add persistent cache on client
[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_inode = NULL;
988         }
989         mutex_init(&lli->lli_layout_mutex);
990         memset(lli->lli_jobid, 0, sizeof(lli->lli_jobid));
991 }
992
993 #define MAX_STRING_SIZE 128
994
995 #ifndef HAVE_SUPER_SETUP_BDI_NAME
996
997 #define LSI_BDI_INITIALIZED     0x00400000
998
999 #ifndef HAVE_BDI_CAP_MAP_COPY
1000 # define BDI_CAP_MAP_COPY       0
1001 #endif
1002
1003 static int super_setup_bdi_name(struct super_block *sb, char *fmt, ...)
1004 {
1005         struct  lustre_sb_info *lsi = s2lsi(sb);
1006         char buf[MAX_STRING_SIZE];
1007         va_list args;
1008         int err;
1009
1010         err = bdi_init(&lsi->lsi_bdi);
1011         if (err)
1012                 return err;
1013
1014         lsi->lsi_flags |= LSI_BDI_INITIALIZED;
1015         lsi->lsi_bdi.capabilities = BDI_CAP_MAP_COPY;
1016         lsi->lsi_bdi.name = "lustre";
1017         va_start(args, fmt);
1018         vsnprintf(buf, MAX_STRING_SIZE, fmt, args);
1019         va_end(args);
1020         err = bdi_register(&lsi->lsi_bdi, NULL, "%s", buf);
1021         va_end(args);
1022         if (!err)
1023                 sb->s_bdi = &lsi->lsi_bdi;
1024
1025         return err;
1026 }
1027 #endif /* !HAVE_SUPER_SETUP_BDI_NAME */
1028
1029 int ll_fill_super(struct super_block *sb, struct vfsmount *mnt)
1030 {
1031         struct  lustre_profile *lprof = NULL;
1032         struct  lustre_sb_info *lsi = s2lsi(sb);
1033         struct  ll_sb_info *sbi = NULL;
1034         char    *dt = NULL, *md = NULL;
1035         char    *profilenm = get_profile_name(sb);
1036         struct config_llog_instance *cfg;
1037         /* %p for void* in printf needs 16+2 characters: 0xffffffffffffffff */
1038         const int instlen = 16 + 2;
1039         unsigned long cfg_instance = ll_get_cfg_instance(sb);
1040         char name[MAX_STRING_SIZE];
1041         int md_len = 0;
1042         int dt_len = 0;
1043         uuid_t uuid;
1044         char *ptr;
1045         int len;
1046         int err;
1047
1048         ENTRY;
1049         /* for ASLR, to map between cfg_instance and hashed ptr */
1050         CDEBUG(D_VFSTRACE, "VFS Op: cfg_instance %s-%016lx (sb %p)\n",
1051                profilenm, cfg_instance, sb);
1052
1053         try_module_get(THIS_MODULE);
1054
1055         OBD_ALLOC_PTR(cfg);
1056         if (cfg == NULL)
1057                 GOTO(out_free_cfg, err = -ENOMEM);
1058
1059         /* client additional sb info */
1060         lsi->lsi_llsbi = sbi = ll_init_sbi();
1061         if (!sbi)
1062                 GOTO(out_free_cfg, err = -ENOMEM);
1063
1064         err = ll_options(lsi->lsi_lmd->lmd_opts, sbi);
1065         if (err)
1066                 GOTO(out_free_cfg, err);
1067
1068 #ifndef HAVE_DCACHE_LOCK
1069         /* kernel >= 2.6.38 store dentry operations in sb->s_d_op. */
1070         sb->s_d_op = &ll_d_ops;
1071 #endif
1072         /* UUID handling */
1073         generate_random_uuid(uuid.b);
1074         snprintf(sbi->ll_sb_uuid.uuid, UUID_SIZE, "%pU", uuid.b);
1075
1076         CDEBUG(D_CONFIG, "llite sb uuid: %s\n", sbi->ll_sb_uuid.uuid);
1077
1078         /* Get fsname */
1079         len = strlen(profilenm);
1080         ptr = strrchr(profilenm, '-');
1081         if (ptr && (strcmp(ptr, "-client") == 0))
1082                 len -= 7;
1083
1084         if (len > LUSTRE_MAXFSNAME) {
1085                 if (unlikely(len >= MAX_STRING_SIZE))
1086                         len = MAX_STRING_SIZE - 1;
1087                 strncpy(name, profilenm, len);
1088                 name[len] = '\0';
1089                 err = -ENAMETOOLONG;
1090                 CERROR("%s: fsname longer than %u characters: rc = %d\n",
1091                        name, LUSTRE_MAXFSNAME, err);
1092                 GOTO(out_free_cfg, err);
1093         }
1094         strncpy(sbi->ll_fsname, profilenm, len);
1095         sbi->ll_fsname[len] = '\0';
1096
1097         /* Mount info */
1098         snprintf(name, sizeof(name), "%.*s-%016lx", len,
1099                  profilenm, cfg_instance);
1100
1101         err = super_setup_bdi_name(sb, "%s", name);
1102         if (err)
1103                 GOTO(out_free_cfg, err);
1104
1105         /* Call ll_debugfs_register_super() before lustre_process_log()
1106          * so that "llite.*.*" params can be processed correctly.
1107          */
1108         err = ll_debugfs_register_super(sb, name);
1109         if (err < 0) {
1110                 CERROR("%s: could not register mountpoint in llite: rc = %d\n",
1111                        sbi->ll_fsname, err);
1112                 err = 0;
1113         }
1114
1115         /* The cfg_instance is a value unique to this super, in case some
1116          * joker tries to mount the same fs at two mount points.
1117          */
1118         cfg->cfg_instance = cfg_instance;
1119         cfg->cfg_uuid = lsi->lsi_llsbi->ll_sb_uuid;
1120         cfg->cfg_callback = class_config_llog_handler;
1121         cfg->cfg_sub_clds = CONFIG_SUB_CLIENT;
1122         /* set up client obds */
1123         err = lustre_process_log(sb, profilenm, cfg);
1124         if (err < 0)
1125                 GOTO(out_debugfs, err);
1126
1127         /* Profile set with LCFG_MOUNTOPT so we can find our mdc and osc obds */
1128         lprof = class_get_profile(profilenm);
1129         if (lprof == NULL) {
1130                 LCONSOLE_ERROR_MSG(0x156, "The client profile '%s' could not be"
1131                                    " read from the MGS.  Does that filesystem "
1132                                    "exist?\n", profilenm);
1133                 GOTO(out_debugfs, err = -EINVAL);
1134         }
1135         CDEBUG(D_CONFIG, "Found profile %s: mdc=%s osc=%s\n", profilenm,
1136                lprof->lp_md, lprof->lp_dt);
1137
1138         dt_len = strlen(lprof->lp_dt) + instlen + 2;
1139         OBD_ALLOC(dt, dt_len);
1140         if (!dt)
1141                 GOTO(out_profile, err = -ENOMEM);
1142         snprintf(dt, dt_len - 1, "%s-%016lx", lprof->lp_dt, cfg_instance);
1143
1144         md_len = strlen(lprof->lp_md) + instlen + 2;
1145         OBD_ALLOC(md, md_len);
1146         if (!md)
1147                 GOTO(out_free_dt, err = -ENOMEM);
1148         snprintf(md, md_len - 1, "%s-%016lx", lprof->lp_md, cfg_instance);
1149
1150         /* connections, registrations, sb setup */
1151         err = client_common_fill_super(sb, md, dt, mnt);
1152         if (err < 0)
1153                 GOTO(out_free_md, err);
1154
1155         sbi->ll_client_common_fill_super_succeeded = 1;
1156
1157 out_free_md:
1158         if (md)
1159                 OBD_FREE(md, md_len);
1160 out_free_dt:
1161         if (dt)
1162                 OBD_FREE(dt, dt_len);
1163 out_profile:
1164         if (lprof)
1165                 class_put_profile(lprof);
1166 out_debugfs:
1167         if (err < 0)
1168                 ll_debugfs_unregister_super(sb);
1169 out_free_cfg:
1170         if (cfg)
1171                 OBD_FREE_PTR(cfg);
1172
1173         if (err)
1174                 ll_put_super(sb);
1175         else if (sbi->ll_flags & LL_SBI_VERBOSE)
1176                 LCONSOLE_WARN("Mounted %s\n", profilenm);
1177         RETURN(err);
1178 } /* ll_fill_super */
1179
1180 void ll_put_super(struct super_block *sb)
1181 {
1182         struct config_llog_instance cfg, params_cfg;
1183         struct obd_device *obd;
1184         struct lustre_sb_info *lsi = s2lsi(sb);
1185         struct ll_sb_info *sbi = ll_s2sbi(sb);
1186         char *profilenm = get_profile_name(sb);
1187         unsigned long cfg_instance = ll_get_cfg_instance(sb);
1188         long ccc_count;
1189         int next, force = 1, rc = 0;
1190         ENTRY;
1191
1192         if (!sbi)
1193                 GOTO(out_no_sbi, 0);
1194
1195         /* Should replace instance_id with something better for ASLR */
1196         CDEBUG(D_VFSTRACE, "VFS Op: cfg_instance %s-%016lx (sb %p)\n",
1197                profilenm, cfg_instance, sb);
1198
1199         cfg.cfg_instance = cfg_instance;
1200         lustre_end_log(sb, profilenm, &cfg);
1201
1202         params_cfg.cfg_instance = cfg_instance;
1203         lustre_end_log(sb, PARAMS_FILENAME, &params_cfg);
1204
1205         if (sbi->ll_md_exp) {
1206                 obd = class_exp2obd(sbi->ll_md_exp);
1207                 if (obd)
1208                         force = obd->obd_force;
1209         }
1210
1211         /* Wait for unstable pages to be committed to stable storage */
1212         if (force == 0) {
1213                 struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
1214                 rc = l_wait_event(sbi->ll_cache->ccc_unstable_waitq,
1215                         atomic_long_read(&sbi->ll_cache->ccc_unstable_nr) == 0,
1216                         &lwi);
1217         }
1218
1219         ccc_count = atomic_long_read(&sbi->ll_cache->ccc_unstable_nr);
1220         if (force == 0 && rc != -EINTR)
1221                 LASSERTF(ccc_count == 0, "count: %li\n", ccc_count);
1222
1223         /* We need to set force before the lov_disconnect in
1224            lustre_common_put_super, since l_d cleans up osc's as well. */
1225         if (force) {
1226                 next = 0;
1227                 while ((obd = class_devices_in_group(&sbi->ll_sb_uuid,
1228                                                      &next)) != NULL) {
1229                         obd->obd_force = force;
1230                 }
1231         }
1232
1233         if (sbi->ll_client_common_fill_super_succeeded) {
1234                 /* Only if client_common_fill_super succeeded */
1235                 client_common_put_super(sb);
1236         }
1237
1238         next = 0;
1239         while ((obd = class_devices_in_group(&sbi->ll_sb_uuid, &next)) !=NULL) {
1240                 class_manual_cleanup(obd);
1241         }
1242
1243         if (sbi->ll_flags & LL_SBI_VERBOSE)
1244                 LCONSOLE_WARN("Unmounted %s\n", profilenm ? profilenm : "");
1245
1246         if (profilenm)
1247                 class_del_profile(profilenm);
1248
1249 #ifndef HAVE_SUPER_SETUP_BDI_NAME
1250         if (lsi->lsi_flags & LSI_BDI_INITIALIZED) {
1251                 bdi_destroy(&lsi->lsi_bdi);
1252                 lsi->lsi_flags &= ~LSI_BDI_INITIALIZED;
1253         }
1254 #endif
1255
1256         ll_free_sbi(sb);
1257         lsi->lsi_llsbi = NULL;
1258 out_no_sbi:
1259         lustre_common_put_super(sb);
1260
1261         cl_env_cache_purge(~0);
1262
1263         module_put(THIS_MODULE);
1264
1265         EXIT;
1266 } /* client_put_super */
1267
1268 struct inode *ll_inode_from_resource_lock(struct ldlm_lock *lock)
1269 {
1270         struct inode *inode = NULL;
1271
1272         /* NOTE: we depend on atomic igrab() -bzzz */
1273         lock_res_and_lock(lock);
1274         if (lock->l_resource->lr_lvb_inode) {
1275                 struct ll_inode_info * lli;
1276                 lli = ll_i2info(lock->l_resource->lr_lvb_inode);
1277                 if (lli->lli_inode_magic == LLI_INODE_MAGIC) {
1278                         inode = igrab(lock->l_resource->lr_lvb_inode);
1279                 } else {
1280                         inode = lock->l_resource->lr_lvb_inode;
1281                         LDLM_DEBUG_LIMIT(inode->i_state & I_FREEING ?  D_INFO :
1282                                          D_WARNING, lock, "lr_lvb_inode %p is "
1283                                          "bogus: magic %08x",
1284                                          lock->l_resource->lr_lvb_inode,
1285                                          lli->lli_inode_magic);
1286                         inode = NULL;
1287                 }
1288         }
1289         unlock_res_and_lock(lock);
1290         return inode;
1291 }
1292
1293 void ll_dir_clear_lsm_md(struct inode *inode)
1294 {
1295         struct ll_inode_info *lli = ll_i2info(inode);
1296
1297         LASSERT(S_ISDIR(inode->i_mode));
1298
1299         if (lli->lli_lsm_md != NULL) {
1300                 lmv_free_memmd(lli->lli_lsm_md);
1301                 lli->lli_lsm_md = NULL;
1302         }
1303 }
1304
1305 static struct inode *ll_iget_anon_dir(struct super_block *sb,
1306                                       const struct lu_fid *fid,
1307                                       struct lustre_md *md)
1308 {
1309         struct ll_sb_info       *sbi = ll_s2sbi(sb);
1310         struct mdt_body         *body = md->body;
1311         struct inode            *inode;
1312         ino_t                   ino;
1313         ENTRY;
1314
1315         ino = cl_fid_build_ino(fid, sbi->ll_flags & LL_SBI_32BIT_API);
1316         inode = iget_locked(sb, ino);
1317         if (inode == NULL) {
1318                 CERROR("%s: failed get simple inode "DFID": rc = -ENOENT\n",
1319                        sbi->ll_fsname, PFID(fid));
1320                 RETURN(ERR_PTR(-ENOENT));
1321         }
1322
1323         if (inode->i_state & I_NEW) {
1324                 struct ll_inode_info *lli = ll_i2info(inode);
1325                 struct lmv_stripe_md *lsm = md->lmv;
1326
1327                 inode->i_mode = (inode->i_mode & ~S_IFMT) |
1328                                 (body->mbo_mode & S_IFMT);
1329                 LASSERTF(S_ISDIR(inode->i_mode), "Not slave inode "DFID"\n",
1330                          PFID(fid));
1331
1332                 inode->i_mtime.tv_sec = 0;
1333                 inode->i_atime.tv_sec = 0;
1334                 inode->i_ctime.tv_sec = 0;
1335                 inode->i_rdev = 0;
1336
1337 #ifdef HAVE_BACKING_DEV_INFO
1338                 /* initializing backing dev info. */
1339                 inode->i_mapping->backing_dev_info =
1340                                                 &s2lsi(inode->i_sb)->lsi_bdi;
1341 #endif
1342                 inode->i_op = &ll_dir_inode_operations;
1343                 inode->i_fop = &ll_dir_operations;
1344                 lli->lli_fid = *fid;
1345                 ll_lli_init(lli);
1346
1347                 LASSERT(lsm != NULL);
1348                 /* master object FID */
1349                 lli->lli_pfid = body->mbo_fid1;
1350                 CDEBUG(D_INODE, "lli %p slave "DFID" master "DFID"\n",
1351                        lli, PFID(fid), PFID(&lli->lli_pfid));
1352                 unlock_new_inode(inode);
1353         }
1354
1355         RETURN(inode);
1356 }
1357
1358 static int ll_init_lsm_md(struct inode *inode, struct lustre_md *md)
1359 {
1360         struct lu_fid *fid;
1361         struct lmv_stripe_md *lsm = md->lmv;
1362         struct ll_inode_info *lli = ll_i2info(inode);
1363         int i;
1364
1365         LASSERT(lsm != NULL);
1366
1367         CDEBUG(D_INODE, "%s: "DFID" set dir layout:\n",
1368                ll_i2sbi(inode)->ll_fsname, PFID(&lli->lli_fid));
1369         lsm_md_dump(D_INODE, lsm);
1370
1371         /* XXX sigh, this lsm_root initialization should be in
1372          * LMV layer, but it needs ll_iget right now, so we
1373          * put this here right now. */
1374         for (i = 0; i < lsm->lsm_md_stripe_count; i++) {
1375                 fid = &lsm->lsm_md_oinfo[i].lmo_fid;
1376                 LASSERT(lsm->lsm_md_oinfo[i].lmo_root == NULL);
1377
1378                 if (!fid_is_sane(fid))
1379                         continue;
1380
1381                 /* Unfortunately ll_iget will call ll_update_inode,
1382                  * where the initialization of slave inode is slightly
1383                  * different, so it reset lsm_md to NULL to avoid
1384                  * initializing lsm for slave inode. */
1385                 lsm->lsm_md_oinfo[i].lmo_root =
1386                                 ll_iget_anon_dir(inode->i_sb, fid, md);
1387                 if (IS_ERR(lsm->lsm_md_oinfo[i].lmo_root)) {
1388                         int rc = PTR_ERR(lsm->lsm_md_oinfo[i].lmo_root);
1389
1390                         lsm->lsm_md_oinfo[i].lmo_root = NULL;
1391                         while (i-- > 0) {
1392                                 iput(lsm->lsm_md_oinfo[i].lmo_root);
1393                                 lsm->lsm_md_oinfo[i].lmo_root = NULL;
1394                         }
1395                         return rc;
1396                 }
1397         }
1398
1399         lli->lli_lsm_md = lsm;
1400
1401         return 0;
1402 }
1403
1404 static int ll_update_lsm_md(struct inode *inode, struct lustre_md *md)
1405 {
1406         struct ll_inode_info *lli = ll_i2info(inode);
1407         struct lmv_stripe_md *lsm = md->lmv;
1408         int rc = 0;
1409
1410         ENTRY;
1411
1412         LASSERT(S_ISDIR(inode->i_mode));
1413         CDEBUG(D_INODE, "update lsm %p of "DFID"\n", lli->lli_lsm_md,
1414                PFID(ll_inode2fid(inode)));
1415
1416         /*
1417          * no striped information from request, lustre_md from req does not
1418          * include stripeEA, see ll_md_setattr()
1419          */
1420         if (!lsm)
1421                 RETURN(0);
1422
1423         /*
1424          * normally dir layout doesn't change, only take read lock to check
1425          * that to avoid blocking other MD operations.
1426          */
1427         if (lli->lli_lsm_md)
1428                 down_read(&lli->lli_lsm_sem);
1429         else
1430                 down_write(&lli->lli_lsm_sem);
1431
1432         /*
1433          * if dir layout mismatch, check whether version is increased, which
1434          * means layout is changed, this happens in dir migration and lfsck.
1435          *
1436          * foreign LMV should not change.
1437          */
1438         if (lli->lli_lsm_md &&
1439             lli->lli_lsm_md->lsm_md_magic != LMV_MAGIC_FOREIGN &&
1440            !lsm_md_eq(lli->lli_lsm_md, lsm)) {
1441                 if (lsm->lsm_md_layout_version <=
1442                     lli->lli_lsm_md->lsm_md_layout_version) {
1443                         CERROR("%s: "DFID" dir layout mismatch:\n",
1444                                ll_i2sbi(inode)->ll_fsname,
1445                                PFID(&lli->lli_fid));
1446                         lsm_md_dump(D_ERROR, lli->lli_lsm_md);
1447                         lsm_md_dump(D_ERROR, lsm);
1448                         GOTO(unlock, rc = -EINVAL);
1449                 }
1450
1451                 /* layout changed, switch to write lock */
1452                 up_read(&lli->lli_lsm_sem);
1453                 down_write(&lli->lli_lsm_sem);
1454                 ll_dir_clear_lsm_md(inode);
1455         }
1456
1457         /* set directory layout */
1458         if (!lli->lli_lsm_md) {
1459                 struct cl_attr  *attr;
1460
1461                 if (lsm->lsm_md_magic == LMV_MAGIC_FOREIGN) {
1462                         /* set md->lmv to NULL, so the following free lustre_md
1463                          * will not free this lsm */
1464                         md->lmv = NULL;
1465                         lli->lli_lsm_md = lsm;
1466                         up_write(&lli->lli_lsm_sem);
1467                         RETURN(0);
1468                 }
1469
1470                 rc = ll_init_lsm_md(inode, md);
1471                 up_write(&lli->lli_lsm_sem);
1472                 if (rc != 0)
1473                         RETURN(rc);
1474
1475                 /* set md->lmv to NULL, so the following free lustre_md
1476                  * will not free this lsm */
1477                 md->lmv = NULL;
1478
1479                 /*
1480                  * md_merge_attr() may take long, since lsm is already set,
1481                  * switch to read lock.
1482                  */
1483                 down_read(&lli->lli_lsm_sem);
1484
1485                 OBD_ALLOC_PTR(attr);
1486                 if (attr == NULL)
1487                         GOTO(unlock, rc = -ENOMEM);
1488
1489                 /* validate the lsm */
1490                 rc = md_merge_attr(ll_i2mdexp(inode), lsm, attr,
1491                                    ll_md_blocking_ast);
1492                 if (rc != 0) {
1493                         OBD_FREE_PTR(attr);
1494                         GOTO(unlock, rc);
1495                 }
1496
1497                 if (md->body->mbo_valid & OBD_MD_FLNLINK)
1498                         md->body->mbo_nlink = attr->cat_nlink;
1499                 if (md->body->mbo_valid & OBD_MD_FLSIZE)
1500                         md->body->mbo_size = attr->cat_size;
1501                 if (md->body->mbo_valid & OBD_MD_FLATIME)
1502                         md->body->mbo_atime = attr->cat_atime;
1503                 if (md->body->mbo_valid & OBD_MD_FLCTIME)
1504                         md->body->mbo_ctime = attr->cat_ctime;
1505                 if (md->body->mbo_valid & OBD_MD_FLMTIME)
1506                         md->body->mbo_mtime = attr->cat_mtime;
1507
1508                 OBD_FREE_PTR(attr);
1509         }
1510 unlock:
1511         up_read(&lli->lli_lsm_sem);
1512
1513         RETURN(rc);
1514 }
1515
1516 void ll_clear_inode(struct inode *inode)
1517 {
1518         struct ll_inode_info *lli = ll_i2info(inode);
1519         struct ll_sb_info *sbi = ll_i2sbi(inode);
1520
1521         ENTRY;
1522
1523         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
1524                PFID(ll_inode2fid(inode)), inode);
1525
1526         if (S_ISDIR(inode->i_mode)) {
1527                 /* these should have been cleared in ll_file_release */
1528                 LASSERT(lli->lli_opendir_key == NULL);
1529                 LASSERT(lli->lli_sai == NULL);
1530                 LASSERT(lli->lli_opendir_pid == 0);
1531         } else {
1532                 pcc_inode_free(inode);
1533         }
1534
1535         md_null_inode(sbi->ll_md_exp, ll_inode2fid(inode));
1536
1537         LASSERT(!lli->lli_open_fd_write_count);
1538         LASSERT(!lli->lli_open_fd_read_count);
1539         LASSERT(!lli->lli_open_fd_exec_count);
1540
1541         if (lli->lli_mds_write_och)
1542                 ll_md_real_close(inode, FMODE_WRITE);
1543         if (lli->lli_mds_exec_och)
1544                 ll_md_real_close(inode, FMODE_EXEC);
1545         if (lli->lli_mds_read_och)
1546                 ll_md_real_close(inode, FMODE_READ);
1547
1548         if (S_ISLNK(inode->i_mode) && lli->lli_symlink_name) {
1549                 OBD_FREE(lli->lli_symlink_name,
1550                          strlen(lli->lli_symlink_name) + 1);
1551                 lli->lli_symlink_name = NULL;
1552         }
1553
1554         ll_xattr_cache_destroy(inode);
1555
1556 #ifdef CONFIG_FS_POSIX_ACL
1557         forget_all_cached_acls(inode);
1558         if (lli->lli_posix_acl) {
1559                 posix_acl_release(lli->lli_posix_acl);
1560                 lli->lli_posix_acl = NULL;
1561         }
1562 #endif
1563         lli->lli_inode_magic = LLI_INODE_DEAD;
1564
1565         if (S_ISDIR(inode->i_mode))
1566                 ll_dir_clear_lsm_md(inode);
1567         else if (S_ISREG(inode->i_mode) && !is_bad_inode(inode))
1568                 LASSERT(list_empty(&lli->lli_agl_list));
1569
1570         /*
1571          * XXX This has to be done before lsm is freed below, because
1572          * cl_object still uses inode lsm.
1573          */
1574         cl_inode_fini(inode);
1575
1576         EXIT;
1577 }
1578
1579 static int ll_md_setattr(struct dentry *dentry, struct md_op_data *op_data)
1580 {
1581         struct lustre_md md;
1582         struct inode *inode = dentry->d_inode;
1583         struct ll_sb_info *sbi = ll_i2sbi(inode);
1584         struct ptlrpc_request *request = NULL;
1585         int rc, ia_valid;
1586         ENTRY;
1587
1588         op_data = ll_prep_md_op_data(op_data, inode, NULL, NULL, 0, 0,
1589                                      LUSTRE_OPC_ANY, NULL);
1590         if (IS_ERR(op_data))
1591                 RETURN(PTR_ERR(op_data));
1592
1593         rc = md_setattr(sbi->ll_md_exp, op_data, NULL, 0, &request);
1594         if (rc) {
1595                 ptlrpc_req_finished(request);
1596                 if (rc == -ENOENT) {
1597                         clear_nlink(inode);
1598                         /* Unlinked special device node? Or just a race?
1599                          * Pretend we done everything. */
1600                         if (!S_ISREG(inode->i_mode) &&
1601                             !S_ISDIR(inode->i_mode)) {
1602                                 ia_valid = op_data->op_attr.ia_valid;
1603                                 op_data->op_attr.ia_valid &= ~TIMES_SET_FLAGS;
1604                                 rc = simple_setattr(dentry, &op_data->op_attr);
1605                                 op_data->op_attr.ia_valid = ia_valid;
1606                         }
1607                 } else if (rc != -EPERM && rc != -EACCES && rc != -ETXTBSY) {
1608                         CERROR("md_setattr fails: rc = %d\n", rc);
1609                 }
1610                 RETURN(rc);
1611         }
1612
1613         rc = md_get_lustre_md(sbi->ll_md_exp, request, sbi->ll_dt_exp,
1614                               sbi->ll_md_exp, &md);
1615         if (rc) {
1616                 ptlrpc_req_finished(request);
1617                 RETURN(rc);
1618         }
1619
1620         ia_valid = op_data->op_attr.ia_valid;
1621         /* inode size will be in ll_setattr_ost, can't do it now since dirty
1622          * cache is not cleared yet. */
1623         op_data->op_attr.ia_valid &= ~(TIMES_SET_FLAGS | ATTR_SIZE);
1624         if (S_ISREG(inode->i_mode))
1625                 inode_lock(inode);
1626         rc = simple_setattr(dentry, &op_data->op_attr);
1627         if (S_ISREG(inode->i_mode))
1628                 inode_unlock(inode);
1629         op_data->op_attr.ia_valid = ia_valid;
1630
1631         rc = ll_update_inode(inode, &md);
1632         ptlrpc_req_finished(request);
1633
1634         RETURN(rc);
1635 }
1636
1637 /* If this inode has objects allocated to it (lsm != NULL), then the OST
1638  * object(s) determine the file size and mtime.  Otherwise, the MDS will
1639  * keep these values until such a time that objects are allocated for it.
1640  * We do the MDS operations first, as it is checking permissions for us.
1641  * We don't to the MDS RPC if there is nothing that we want to store there,
1642  * otherwise there is no harm in updating mtime/atime on the MDS if we are
1643  * going to do an RPC anyways.
1644  *
1645  * If we are doing a truncate, we will send the mtime and ctime updates
1646  * to the OST with the punch RPC, otherwise we do an explicit setattr RPC.
1647  * I don't believe it is possible to get e.g. ATTR_MTIME_SET and ATTR_SIZE
1648  * at the same time.
1649  *
1650  * In case of HSMimport, we only set attr on MDS.
1651  */
1652 int ll_setattr_raw(struct dentry *dentry, struct iattr *attr,
1653                    enum op_xvalid xvalid, bool hsm_import)
1654 {
1655         struct inode *inode = dentry->d_inode;
1656         struct ll_inode_info *lli = ll_i2info(inode);
1657         struct md_op_data *op_data = NULL;
1658         int rc = 0;
1659         ENTRY;
1660
1661         CDEBUG(D_VFSTRACE, "%s: setattr inode "DFID"(%p) from %llu to %llu, "
1662                "valid %x, hsm_import %d\n",
1663                ll_i2sbi(inode)->ll_fsname, PFID(&lli->lli_fid),
1664                inode, i_size_read(inode), attr->ia_size, attr->ia_valid,
1665                hsm_import);
1666
1667         if (attr->ia_valid & ATTR_SIZE) {
1668                 /* Check new size against VFS/VM file size limit and rlimit */
1669                 rc = inode_newsize_ok(inode, attr->ia_size);
1670                 if (rc)
1671                         RETURN(rc);
1672
1673                 /* The maximum Lustre file size is variable, based on the
1674                  * OST maximum object size and number of stripes.  This
1675                  * needs another check in addition to the VFS check above. */
1676                 if (attr->ia_size > ll_file_maxbytes(inode)) {
1677                         CDEBUG(D_INODE,"file "DFID" too large %llu > %llu\n",
1678                                PFID(&lli->lli_fid), attr->ia_size,
1679                                ll_file_maxbytes(inode));
1680                         RETURN(-EFBIG);
1681                 }
1682
1683                 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
1684         }
1685
1686         /* POSIX: check before ATTR_*TIME_SET set (from inode_change_ok) */
1687         if (attr->ia_valid & TIMES_SET_FLAGS) {
1688                 if ((!uid_eq(current_fsuid(), inode->i_uid)) &&
1689                     !cfs_capable(CFS_CAP_FOWNER))
1690                         RETURN(-EPERM);
1691         }
1692
1693         /* We mark all of the fields "set" so MDS/OST does not re-set them */
1694         if (!(xvalid & OP_XVALID_CTIME_SET) &&
1695              (attr->ia_valid & ATTR_CTIME)) {
1696                 attr->ia_ctime = current_time(inode);
1697                 xvalid |= OP_XVALID_CTIME_SET;
1698         }
1699         if (!(attr->ia_valid & ATTR_ATIME_SET) &&
1700             (attr->ia_valid & ATTR_ATIME)) {
1701                 attr->ia_atime = current_time(inode);
1702                 attr->ia_valid |= ATTR_ATIME_SET;
1703         }
1704         if (!(attr->ia_valid & ATTR_MTIME_SET) &&
1705             (attr->ia_valid & ATTR_MTIME)) {
1706                 attr->ia_mtime = current_time(inode);
1707                 attr->ia_valid |= ATTR_MTIME_SET;
1708         }
1709
1710         if (attr->ia_valid & (ATTR_MTIME | ATTR_CTIME))
1711                 CDEBUG(D_INODE, "setting mtime %lld, ctime %lld, now = %lld\n",
1712                        (s64)attr->ia_mtime.tv_sec, (s64)attr->ia_ctime.tv_sec,
1713                        ktime_get_real_seconds());
1714
1715         if (S_ISREG(inode->i_mode)) {
1716                 if (attr->ia_valid & ATTR_SIZE)
1717                         inode_dio_write_done(inode);
1718                 inode_unlock(inode);
1719         }
1720
1721         /* We always do an MDS RPC, even if we're only changing the size;
1722          * only the MDS knows whether truncate() should fail with -ETXTBUSY */
1723
1724         OBD_ALLOC_PTR(op_data);
1725         if (op_data == NULL)
1726                 GOTO(out, rc = -ENOMEM);
1727
1728         if (!hsm_import && attr->ia_valid & ATTR_SIZE) {
1729                 /* If we are changing file size, file content is
1730                  * modified, flag it.
1731                  */
1732                 xvalid |= OP_XVALID_OWNEROVERRIDE;
1733                 op_data->op_bias |= MDS_DATA_MODIFIED;
1734                 ll_file_clear_flag(lli, LLIF_DATA_MODIFIED);
1735         }
1736
1737         if (attr->ia_valid & ATTR_FILE) {
1738                 struct ll_file_data *fd = LUSTRE_FPRIVATE(attr->ia_file);
1739
1740                 if (fd->fd_lease_och)
1741                         op_data->op_bias |= MDS_TRUNC_KEEP_LEASE;
1742         }
1743
1744         op_data->op_attr = *attr;
1745         op_data->op_xvalid = xvalid;
1746
1747         rc = ll_md_setattr(dentry, op_data);
1748         if (rc)
1749                 GOTO(out, rc);
1750
1751         if (!S_ISREG(inode->i_mode) || hsm_import)
1752                 GOTO(out, rc = 0);
1753
1754         if (attr->ia_valid & (ATTR_SIZE | ATTR_ATIME | ATTR_ATIME_SET |
1755                               ATTR_MTIME | ATTR_MTIME_SET | ATTR_CTIME) ||
1756             xvalid & OP_XVALID_CTIME_SET) {
1757                 bool cached = false;
1758
1759                 rc = pcc_inode_setattr(inode, attr, &cached);
1760                 if (cached) {
1761                         if (rc) {
1762                                 CERROR("%s: PCC inode "DFID" setattr failed: "
1763                                        "rc = %d\n",
1764                                        ll_i2sbi(inode)->ll_fsname,
1765                                        PFID(&lli->lli_fid), rc);
1766                                 GOTO(out, rc);
1767                         }
1768                 } else {
1769                         /* For truncate and utimes sending attributes to OSTs,
1770                          * setting mtime/atime to the past will be performed
1771                          * under PW [0:EOF] extent lock (new_size:EOF for
1772                          * truncate). It may seem excessive to send mtime/atime
1773                          * updates to OSTs when not setting times to past, but
1774                          * it is necessary due to possible time
1775                          * de-synchronization between MDT inode and OST objects
1776                          */
1777                         rc = cl_setattr_ost(lli->lli_clob, attr, xvalid, 0);
1778                 }
1779         }
1780
1781         /* If the file was restored, it needs to set dirty flag.
1782          *
1783          * We've already sent MDS_DATA_MODIFIED flag in
1784          * ll_md_setattr() for truncate. However, the MDT refuses to
1785          * set the HS_DIRTY flag on released files, so we have to set
1786          * it again if the file has been restored. Please check how
1787          * LLIF_DATA_MODIFIED is set in vvp_io_setattr_fini().
1788          *
1789          * Please notice that if the file is not released, the previous
1790          * MDS_DATA_MODIFIED has taken effect and usually
1791          * LLIF_DATA_MODIFIED is not set(see vvp_io_setattr_fini()).
1792          * This way we can save an RPC for common open + trunc
1793          * operation. */
1794         if (ll_file_test_and_clear_flag(lli, LLIF_DATA_MODIFIED)) {
1795                 struct hsm_state_set hss = {
1796                         .hss_valid = HSS_SETMASK,
1797                         .hss_setmask = HS_DIRTY,
1798                 };
1799                 int rc2;
1800
1801                 rc2 = ll_hsm_state_set(inode, &hss);
1802                 /* truncate and write can happen at the same time, so that
1803                  * the file can be set modified even though the file is not
1804                  * restored from released state, and ll_hsm_state_set() is
1805                  * not applicable for the file, and rc2 < 0 is normal in this
1806                  * case. */
1807                 if (rc2 < 0)
1808                         CDEBUG(D_INFO, DFID "HSM set dirty failed: rc2 = %d\n",
1809                                PFID(ll_inode2fid(inode)), rc2);
1810         }
1811
1812         EXIT;
1813 out:
1814         if (op_data != NULL)
1815                 ll_finish_md_op_data(op_data);
1816
1817         if (S_ISREG(inode->i_mode)) {
1818                 inode_lock(inode);
1819                 if ((attr->ia_valid & ATTR_SIZE) && !hsm_import)
1820                         inode_dio_wait(inode);
1821                 /* Once we've got the i_mutex, it's safe to set the S_NOSEC
1822                  * flag.  ll_update_inode (called from ll_md_setattr), clears
1823                  * inode flags, so there is a gap where S_NOSEC is not set.
1824                  * This can cause a writer to take the i_mutex unnecessarily,
1825                  * but this is safe to do and should be rare. */
1826                 inode_has_no_xattr(inode);
1827         }
1828
1829         ll_stats_ops_tally(ll_i2sbi(inode), (attr->ia_valid & ATTR_SIZE) ?
1830                         LPROC_LL_TRUNC : LPROC_LL_SETATTR, 1);
1831
1832         return rc;
1833 }
1834
1835 int ll_setattr(struct dentry *de, struct iattr *attr)
1836 {
1837         int mode = de->d_inode->i_mode;
1838         enum op_xvalid xvalid = 0;
1839
1840         if ((attr->ia_valid & (ATTR_CTIME|ATTR_SIZE|ATTR_MODE)) ==
1841                               (ATTR_CTIME|ATTR_SIZE|ATTR_MODE))
1842                 xvalid |= OP_XVALID_OWNEROVERRIDE;
1843
1844         if (((attr->ia_valid & (ATTR_MODE|ATTR_FORCE|ATTR_SIZE)) ==
1845                                (ATTR_SIZE|ATTR_MODE)) &&
1846             (((mode & S_ISUID) && !(attr->ia_mode & S_ISUID)) ||
1847              (((mode & (S_ISGID|S_IXGRP)) == (S_ISGID|S_IXGRP)) &&
1848               !(attr->ia_mode & S_ISGID))))
1849                 attr->ia_valid |= ATTR_FORCE;
1850
1851         if ((attr->ia_valid & ATTR_MODE) &&
1852             (mode & S_ISUID) &&
1853             !(attr->ia_mode & S_ISUID) &&
1854             !(attr->ia_valid & ATTR_KILL_SUID))
1855                 attr->ia_valid |= ATTR_KILL_SUID;
1856
1857         if ((attr->ia_valid & ATTR_MODE) &&
1858             ((mode & (S_ISGID|S_IXGRP)) == (S_ISGID|S_IXGRP)) &&
1859             !(attr->ia_mode & S_ISGID) &&
1860             !(attr->ia_valid & ATTR_KILL_SGID))
1861                 attr->ia_valid |= ATTR_KILL_SGID;
1862
1863         return ll_setattr_raw(de, attr, xvalid, false);
1864 }
1865
1866 int ll_statfs_internal(struct ll_sb_info *sbi, struct obd_statfs *osfs,
1867                        u32 flags)
1868 {
1869         struct obd_statfs obd_osfs = { 0 };
1870         time64_t max_age;
1871         int rc;
1872
1873         ENTRY;
1874         max_age = ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS;
1875
1876         rc = obd_statfs(NULL, sbi->ll_md_exp, osfs, max_age, flags);
1877         if (rc)
1878                 RETURN(rc);
1879
1880         osfs->os_type = LL_SUPER_MAGIC;
1881
1882         CDEBUG(D_SUPER, "MDC blocks %llu/%llu objects %llu/%llu\n",
1883               osfs->os_bavail, osfs->os_blocks, osfs->os_ffree, osfs->os_files);
1884
1885         if (osfs->os_state & OS_STATE_SUM)
1886                 GOTO(out, rc);
1887
1888         if (sbi->ll_flags & LL_SBI_LAZYSTATFS)
1889                 flags |= OBD_STATFS_NODELAY;
1890
1891         rc = obd_statfs(NULL, sbi->ll_dt_exp, &obd_osfs, max_age, flags);
1892         if (rc) /* Possibly a filesystem with no OSTs.  Report MDT totals. */
1893                 GOTO(out, rc = 0);
1894
1895         CDEBUG(D_SUPER, "OSC blocks %llu/%llu objects %llu/%llu\n",
1896                obd_osfs.os_bavail, obd_osfs.os_blocks, obd_osfs.os_ffree,
1897                obd_osfs.os_files);
1898
1899         osfs->os_bsize = obd_osfs.os_bsize;
1900         osfs->os_blocks = obd_osfs.os_blocks;
1901         osfs->os_bfree = obd_osfs.os_bfree;
1902         osfs->os_bavail = obd_osfs.os_bavail;
1903
1904         /* If we have _some_ OSTs, but don't have as many free objects on the
1905          * OSTs as inodes on the MDTs, reduce the reported number of inodes
1906          * to compensate, so that the "inodes in use" number is correct.
1907          * This should be kept in sync with lod_statfs() behaviour.
1908          */
1909         if (obd_osfs.os_files && obd_osfs.os_ffree < osfs->os_ffree) {
1910                 osfs->os_files = (osfs->os_files - osfs->os_ffree) +
1911                                  obd_osfs.os_ffree;
1912                 osfs->os_ffree = obd_osfs.os_ffree;
1913         }
1914
1915 out:
1916         RETURN(rc);
1917 }
1918 int ll_statfs(struct dentry *de, struct kstatfs *sfs)
1919 {
1920         struct super_block *sb = de->d_sb;
1921         struct obd_statfs osfs;
1922         __u64 fsid = huge_encode_dev(sb->s_dev);
1923         int rc;
1924
1925         CDEBUG(D_VFSTRACE, "VFS Op: at %llu jiffies\n", get_jiffies_64());
1926         ll_stats_ops_tally(ll_s2sbi(sb), LPROC_LL_STAFS, 1);
1927
1928         /* Some amount of caching on the client is allowed */
1929         rc = ll_statfs_internal(ll_s2sbi(sb), &osfs, OBD_STATFS_SUM);
1930         if (rc)
1931                 return rc;
1932
1933         statfs_unpack(sfs, &osfs);
1934
1935         /* We need to downshift for all 32-bit kernels, because we can't
1936          * tell if the kernel is being called via sys_statfs64() or not.
1937          * Stop before overflowing f_bsize - in which case it is better
1938          * to just risk EOVERFLOW if caller is using old sys_statfs(). */
1939         if (sizeof(long) < 8) {
1940                 while (osfs.os_blocks > ~0UL && sfs->f_bsize < 0x40000000) {
1941                         sfs->f_bsize <<= 1;
1942
1943                         osfs.os_blocks >>= 1;
1944                         osfs.os_bfree >>= 1;
1945                         osfs.os_bavail >>= 1;
1946                 }
1947         }
1948
1949         sfs->f_blocks = osfs.os_blocks;
1950         sfs->f_bfree = osfs.os_bfree;
1951         sfs->f_bavail = osfs.os_bavail;
1952         sfs->f_fsid.val[0] = (__u32)fsid;
1953         sfs->f_fsid.val[1] = (__u32)(fsid >> 32);
1954         return 0;
1955 }
1956
1957 void ll_inode_size_lock(struct inode *inode)
1958 {
1959         struct ll_inode_info *lli;
1960
1961         LASSERT(!S_ISDIR(inode->i_mode));
1962
1963         lli = ll_i2info(inode);
1964         mutex_lock(&lli->lli_size_mutex);
1965 }
1966
1967 void ll_inode_size_unlock(struct inode *inode)
1968 {
1969         struct ll_inode_info *lli;
1970
1971         lli = ll_i2info(inode);
1972         mutex_unlock(&lli->lli_size_mutex);
1973 }
1974
1975 void ll_update_inode_flags(struct inode *inode, int ext_flags)
1976 {
1977         inode->i_flags = ll_ext_to_inode_flags(ext_flags);
1978         if (ext_flags & LUSTRE_PROJINHERIT_FL)
1979                 ll_file_set_flag(ll_i2info(inode), LLIF_PROJECT_INHERIT);
1980         else
1981                 ll_file_clear_flag(ll_i2info(inode), LLIF_PROJECT_INHERIT);
1982 }
1983
1984 int ll_update_inode(struct inode *inode, struct lustre_md *md)
1985 {
1986         struct ll_inode_info *lli = ll_i2info(inode);
1987         struct mdt_body *body = md->body;
1988         struct ll_sb_info *sbi = ll_i2sbi(inode);
1989         int rc = 0;
1990
1991         if (body->mbo_valid & OBD_MD_FLEASIZE) {
1992                 rc = cl_file_inode_init(inode, md);
1993                 if (rc)
1994                         return rc;
1995         }
1996
1997         if (S_ISDIR(inode->i_mode)) {
1998                 rc = ll_update_lsm_md(inode, md);
1999                 if (rc != 0)
2000                         return rc;
2001         }
2002
2003 #ifdef CONFIG_FS_POSIX_ACL
2004         if (body->mbo_valid & OBD_MD_FLACL) {
2005                 spin_lock(&lli->lli_lock);
2006                 if (lli->lli_posix_acl)
2007                         posix_acl_release(lli->lli_posix_acl);
2008                 lli->lli_posix_acl = md->posix_acl;
2009                 spin_unlock(&lli->lli_lock);
2010         }
2011 #endif
2012         inode->i_ino = cl_fid_build_ino(&body->mbo_fid1,
2013                                         sbi->ll_flags & LL_SBI_32BIT_API);
2014         inode->i_generation = cl_fid_build_gen(&body->mbo_fid1);
2015
2016         if (body->mbo_valid & OBD_MD_FLATIME) {
2017                 if (body->mbo_atime > inode->i_atime.tv_sec)
2018                         inode->i_atime.tv_sec = body->mbo_atime;
2019                 lli->lli_atime = body->mbo_atime;
2020         }
2021
2022         if (body->mbo_valid & OBD_MD_FLMTIME) {
2023                 if (body->mbo_mtime > inode->i_mtime.tv_sec) {
2024                         CDEBUG(D_INODE,
2025                                "setting ino %lu mtime from %lld to %llu\n",
2026                                inode->i_ino, (s64)inode->i_mtime.tv_sec,
2027                                body->mbo_mtime);
2028                         inode->i_mtime.tv_sec = body->mbo_mtime;
2029                 }
2030                 lli->lli_mtime = body->mbo_mtime;
2031         }
2032
2033         if (body->mbo_valid & OBD_MD_FLCTIME) {
2034                 if (body->mbo_ctime > inode->i_ctime.tv_sec)
2035                         inode->i_ctime.tv_sec = body->mbo_ctime;
2036                 lli->lli_ctime = body->mbo_ctime;
2037         }
2038
2039         /* Clear i_flags to remove S_NOSEC before permissions are updated */
2040         if (body->mbo_valid & OBD_MD_FLFLAGS)
2041                 ll_update_inode_flags(inode, body->mbo_flags);
2042         if (body->mbo_valid & OBD_MD_FLMODE)
2043                 inode->i_mode = (inode->i_mode & S_IFMT) |
2044                                 (body->mbo_mode & ~S_IFMT);
2045
2046         if (body->mbo_valid & OBD_MD_FLTYPE)
2047                 inode->i_mode = (inode->i_mode & ~S_IFMT) |
2048                                 (body->mbo_mode & S_IFMT);
2049
2050         LASSERT(inode->i_mode != 0);
2051         if (S_ISREG(inode->i_mode))
2052                 inode->i_blkbits = min(PTLRPC_MAX_BRW_BITS + 1,
2053                                        LL_MAX_BLKSIZE_BITS);
2054         else
2055                 inode->i_blkbits = inode->i_sb->s_blocksize_bits;
2056
2057         if (body->mbo_valid & OBD_MD_FLUID)
2058                 inode->i_uid = make_kuid(&init_user_ns, body->mbo_uid);
2059         if (body->mbo_valid & OBD_MD_FLGID)
2060                 inode->i_gid = make_kgid(&init_user_ns, body->mbo_gid);
2061         if (body->mbo_valid & OBD_MD_FLPROJID)
2062                 lli->lli_projid = body->mbo_projid;
2063         if (body->mbo_valid & OBD_MD_FLNLINK)
2064                 set_nlink(inode, body->mbo_nlink);
2065         if (body->mbo_valid & OBD_MD_FLRDEV)
2066                 inode->i_rdev = old_decode_dev(body->mbo_rdev);
2067
2068         if (body->mbo_valid & OBD_MD_FLID) {
2069                 /* FID shouldn't be changed! */
2070                 if (fid_is_sane(&lli->lli_fid)) {
2071                         LASSERTF(lu_fid_eq(&lli->lli_fid, &body->mbo_fid1),
2072                                  "Trying to change FID "DFID
2073                                  " to the "DFID", inode "DFID"(%p)\n",
2074                                  PFID(&lli->lli_fid), PFID(&body->mbo_fid1),
2075                                  PFID(ll_inode2fid(inode)), inode);
2076                 } else {
2077                         lli->lli_fid = body->mbo_fid1;
2078                 }
2079         }
2080
2081         LASSERT(fid_seq(&lli->lli_fid) != 0);
2082
2083         if (body->mbo_valid & OBD_MD_FLSIZE) {
2084                 i_size_write(inode, body->mbo_size);
2085
2086                 CDEBUG(D_VFSTRACE, "inode="DFID", updating i_size %llu\n",
2087                        PFID(ll_inode2fid(inode)),
2088                        (unsigned long long)body->mbo_size);
2089
2090                 if (body->mbo_valid & OBD_MD_FLBLOCKS)
2091                         inode->i_blocks = body->mbo_blocks;
2092         }
2093
2094         if (body->mbo_valid & OBD_MD_TSTATE) {
2095                 /* Set LLIF_FILE_RESTORING if restore ongoing and
2096                  * clear it when done to ensure to start again
2097                  * glimpsing updated attrs
2098                  */
2099                 if (body->mbo_t_state & MS_RESTORE)
2100                         ll_file_set_flag(lli, LLIF_FILE_RESTORING);
2101                 else
2102                         ll_file_clear_flag(lli, LLIF_FILE_RESTORING);
2103         }
2104
2105         return 0;
2106 }
2107
2108 int ll_read_inode2(struct inode *inode, void *opaque)
2109 {
2110         struct lustre_md *md = opaque;
2111         struct ll_inode_info *lli = ll_i2info(inode);
2112         int     rc;
2113         ENTRY;
2114
2115         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
2116                PFID(&lli->lli_fid), inode);
2117
2118         /* Core attributes from the MDS first.  This is a new inode, and
2119          * the VFS doesn't zero times in the core inode so we have to do
2120          * it ourselves.  They will be overwritten by either MDS or OST
2121          * attributes - we just need to make sure they aren't newer.
2122          */
2123         inode->i_mtime.tv_sec = 0;
2124         inode->i_atime.tv_sec = 0;
2125         inode->i_ctime.tv_sec = 0;
2126         inode->i_rdev = 0;
2127         rc = ll_update_inode(inode, md);
2128         if (rc != 0)
2129                 RETURN(rc);
2130
2131         /* OIDEBUG(inode); */
2132
2133 #ifdef HAVE_BACKING_DEV_INFO
2134         /* initializing backing dev info. */
2135         inode->i_mapping->backing_dev_info = &s2lsi(inode->i_sb)->lsi_bdi;
2136 #endif
2137         if (S_ISREG(inode->i_mode)) {
2138                 struct ll_sb_info *sbi = ll_i2sbi(inode);
2139                 inode->i_op = &ll_file_inode_operations;
2140                 inode->i_fop = sbi->ll_fop;
2141                 inode->i_mapping->a_ops = (struct address_space_operations *)&ll_aops;
2142                 EXIT;
2143         } else if (S_ISDIR(inode->i_mode)) {
2144                 inode->i_op = &ll_dir_inode_operations;
2145                 inode->i_fop = &ll_dir_operations;
2146                 EXIT;
2147         } else if (S_ISLNK(inode->i_mode)) {
2148                 inode->i_op = &ll_fast_symlink_inode_operations;
2149                 EXIT;
2150         } else {
2151                 inode->i_op = &ll_special_inode_operations;
2152
2153                 init_special_inode(inode, inode->i_mode,
2154                                    inode->i_rdev);
2155
2156                 EXIT;
2157         }
2158
2159         return 0;
2160 }
2161
2162 void ll_delete_inode(struct inode *inode)
2163 {
2164         struct ll_inode_info *lli = ll_i2info(inode);
2165         struct address_space *mapping = &inode->i_data;
2166         unsigned long nrpages;
2167         ENTRY;
2168
2169         if (S_ISREG(inode->i_mode) && lli->lli_clob != NULL) {
2170                 /* It is last chance to write out dirty pages,
2171                  * otherwise we may lose data while umount.
2172                  *
2173                  * If i_nlink is 0 then just discard data. This is safe because
2174                  * local inode gets i_nlink 0 from server only for the last
2175                  * unlink, so that file is not opened somewhere else
2176                  */
2177                 cl_sync_file_range(inode, 0, OBD_OBJECT_EOF, inode->i_nlink ?
2178                                    CL_FSYNC_LOCAL : CL_FSYNC_DISCARD, 1);
2179         }
2180         truncate_inode_pages_final(mapping);
2181
2182         /* Workaround for LU-118: Note nrpages may not be totally updated when
2183          * truncate_inode_pages() returns, as there can be a page in the process
2184          * of deletion (inside __delete_from_page_cache()) in the specified
2185          * range. Thus mapping->nrpages can be non-zero when this function
2186          * returns even after truncation of the whole mapping.  Only do this if
2187          * npages isn't already zero.
2188          */
2189         nrpages = mapping->nrpages;
2190         if (nrpages) {
2191                 xa_lock_irq(&mapping->i_pages);
2192                 nrpages = mapping->nrpages;
2193                 xa_unlock_irq(&mapping->i_pages);
2194         } /* Workaround end */
2195
2196         LASSERTF(nrpages == 0, "%s: inode="DFID"(%p) nrpages=%lu, "
2197                  "see https://jira.whamcloud.com/browse/LU-118\n",
2198                  ll_i2sbi(inode)->ll_fsname,
2199                  PFID(ll_inode2fid(inode)), inode, nrpages);
2200
2201 #ifdef HAVE_SBOPS_EVICT_INODE
2202         ll_clear_inode(inode);
2203 #endif
2204         clear_inode(inode);
2205
2206         EXIT;
2207 }
2208
2209 int ll_iocontrol(struct inode *inode, struct file *file,
2210                  unsigned int cmd, unsigned long arg)
2211 {
2212         struct ll_sb_info *sbi = ll_i2sbi(inode);
2213         struct ptlrpc_request *req = NULL;
2214         int rc, flags = 0;
2215         ENTRY;
2216
2217         switch (cmd) {
2218         case FS_IOC_GETFLAGS: {
2219                 struct mdt_body *body;
2220                 struct md_op_data *op_data;
2221
2222                 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL,
2223                                              0, 0, LUSTRE_OPC_ANY,
2224                                              NULL);
2225                 if (IS_ERR(op_data))
2226                         RETURN(PTR_ERR(op_data));
2227
2228                 op_data->op_valid = OBD_MD_FLFLAGS;
2229                 rc = md_getattr(sbi->ll_md_exp, op_data, &req);
2230                 ll_finish_md_op_data(op_data);
2231                 if (rc) {
2232                         CERROR("%s: failure inode "DFID": rc = %d\n",
2233                                sbi->ll_md_exp->exp_obd->obd_name,
2234                                PFID(ll_inode2fid(inode)), rc);
2235                         RETURN(-abs(rc));
2236                 }
2237
2238                 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
2239
2240                 flags = body->mbo_flags;
2241
2242                 ptlrpc_req_finished(req);
2243
2244                 RETURN(put_user(flags, (int __user *)arg));
2245         }
2246         case FS_IOC_SETFLAGS: {
2247                 struct iattr *attr;
2248                 struct md_op_data *op_data;
2249                 struct cl_object *obj;
2250                 struct fsxattr fa = { 0 };
2251
2252                 if (get_user(flags, (int __user *)arg))
2253                         RETURN(-EFAULT);
2254
2255                 fa.fsx_projid = ll_i2info(inode)->lli_projid;
2256                 if (flags & LUSTRE_PROJINHERIT_FL)
2257                         fa.fsx_xflags = FS_XFLAG_PROJINHERIT;
2258
2259                 rc = ll_ioctl_check_project(inode, &fa);
2260                 if (rc)
2261                         RETURN(rc);
2262
2263                 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
2264                                              LUSTRE_OPC_ANY, NULL);
2265                 if (IS_ERR(op_data))
2266                         RETURN(PTR_ERR(op_data));
2267
2268                 op_data->op_attr_flags = flags;
2269                 op_data->op_xvalid |= OP_XVALID_FLAGS;
2270                 rc = md_setattr(sbi->ll_md_exp, op_data, NULL, 0, &req);
2271                 ll_finish_md_op_data(op_data);
2272                 ptlrpc_req_finished(req);
2273                 if (rc)
2274                         RETURN(rc);
2275
2276                 ll_update_inode_flags(inode, flags);
2277
2278                 obj = ll_i2info(inode)->lli_clob;
2279                 if (obj == NULL)
2280                         RETURN(0);
2281
2282                 OBD_ALLOC_PTR(attr);
2283                 if (attr == NULL)
2284                         RETURN(-ENOMEM);
2285
2286                 rc = cl_setattr_ost(obj, attr, OP_XVALID_FLAGS, flags);
2287
2288                 OBD_FREE_PTR(attr);
2289                 RETURN(rc);
2290         }
2291         default:
2292                 RETURN(-ENOSYS);
2293         }
2294
2295         RETURN(0);
2296 }
2297
2298 int ll_flush_ctx(struct inode *inode)
2299 {
2300         struct ll_sb_info  *sbi = ll_i2sbi(inode);
2301
2302         CDEBUG(D_SEC, "flush context for user %d\n",
2303                from_kuid(&init_user_ns, current_uid()));
2304
2305         obd_set_info_async(NULL, sbi->ll_md_exp,
2306                            sizeof(KEY_FLUSH_CTX), KEY_FLUSH_CTX,
2307                            0, NULL, NULL);
2308         obd_set_info_async(NULL, sbi->ll_dt_exp,
2309                            sizeof(KEY_FLUSH_CTX), KEY_FLUSH_CTX,
2310                            0, NULL, NULL);
2311         return 0;
2312 }
2313
2314 /* umount -f client means force down, don't save state */
2315 void ll_umount_begin(struct super_block *sb)
2316 {
2317         struct ll_sb_info *sbi = ll_s2sbi(sb);
2318         struct obd_device *obd;
2319         struct obd_ioctl_data *ioc_data;
2320         struct l_wait_info lwi;
2321         wait_queue_head_t waitq;
2322         ENTRY;
2323
2324         CDEBUG(D_VFSTRACE, "VFS Op: superblock %p count %d active %d\n", sb,
2325                sb->s_count, atomic_read(&sb->s_active));
2326
2327         obd = class_exp2obd(sbi->ll_md_exp);
2328         if (obd == NULL) {
2329                 CERROR("Invalid MDC connection handle %#llx\n",
2330                        sbi->ll_md_exp->exp_handle.h_cookie);
2331                 EXIT;
2332                 return;
2333         }
2334         obd->obd_force = 1;
2335
2336         obd = class_exp2obd(sbi->ll_dt_exp);
2337         if (obd == NULL) {
2338                 CERROR("Invalid LOV connection handle %#llx\n",
2339                        sbi->ll_dt_exp->exp_handle.h_cookie);
2340                 EXIT;
2341                 return;
2342         }
2343         obd->obd_force = 1;
2344
2345         OBD_ALLOC_PTR(ioc_data);
2346         if (ioc_data) {
2347                 obd_iocontrol(IOC_OSC_SET_ACTIVE, sbi->ll_md_exp,
2348                               sizeof *ioc_data, ioc_data, NULL);
2349
2350                 obd_iocontrol(IOC_OSC_SET_ACTIVE, sbi->ll_dt_exp,
2351                               sizeof *ioc_data, ioc_data, NULL);
2352
2353                 OBD_FREE_PTR(ioc_data);
2354         }
2355
2356         /* Really, we'd like to wait until there are no requests outstanding,
2357          * and then continue.  For now, we just periodically checking for vfs
2358          * to decrement mnt_cnt and hope to finish it within 10sec.
2359          */
2360         init_waitqueue_head(&waitq);
2361         lwi = LWI_TIMEOUT_INTERVAL(cfs_time_seconds(10),
2362                                    cfs_time_seconds(1), NULL, NULL);
2363         l_wait_event(waitq, may_umount(sbi->ll_mnt.mnt), &lwi);
2364
2365         EXIT;
2366 }
2367
2368 int ll_remount_fs(struct super_block *sb, int *flags, char *data)
2369 {
2370         struct ll_sb_info *sbi = ll_s2sbi(sb);
2371         char *profilenm = get_profile_name(sb);
2372         int err;
2373         __u32 read_only;
2374
2375         if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY)) {
2376                 read_only = *flags & MS_RDONLY;
2377                 err = obd_set_info_async(NULL, sbi->ll_md_exp,
2378                                          sizeof(KEY_READ_ONLY),
2379                                          KEY_READ_ONLY, sizeof(read_only),
2380                                          &read_only, NULL);
2381                 if (err) {
2382                         LCONSOLE_WARN("Failed to remount %s %s (%d)\n",
2383                                       profilenm, read_only ?
2384                                       "read-only" : "read-write", err);
2385                         return err;
2386                 }
2387
2388                 if (read_only)
2389                         sb->s_flags |= MS_RDONLY;
2390                 else
2391                         sb->s_flags &= ~MS_RDONLY;
2392
2393                 if (sbi->ll_flags & LL_SBI_VERBOSE)
2394                         LCONSOLE_WARN("Remounted %s %s\n", profilenm,
2395                                       read_only ?  "read-only" : "read-write");
2396         }
2397         return 0;
2398 }
2399
2400 /**
2401  * Cleanup the open handle that is cached on MDT-side.
2402  *
2403  * For open case, the client side open handling thread may hit error
2404  * after the MDT grant the open. Under such case, the client should
2405  * send close RPC to the MDT as cleanup; otherwise, the open handle
2406  * on the MDT will be leaked there until the client umount or evicted.
2407  *
2408  * In further, if someone unlinked the file, because the open handle
2409  * holds the reference on such file/object, then it will block the
2410  * subsequent threads that want to locate such object via FID.
2411  *
2412  * \param[in] sb        super block for this file-system
2413  * \param[in] open_req  pointer to the original open request
2414  */
2415 void ll_open_cleanup(struct super_block *sb, struct ptlrpc_request *open_req)
2416 {
2417         struct mdt_body                 *body;
2418         struct md_op_data               *op_data;
2419         struct ptlrpc_request           *close_req = NULL;
2420         struct obd_export               *exp       = ll_s2sbi(sb)->ll_md_exp;
2421         ENTRY;
2422
2423         body = req_capsule_server_get(&open_req->rq_pill, &RMF_MDT_BODY);
2424         OBD_ALLOC_PTR(op_data);
2425         if (op_data == NULL) {
2426                 CWARN("%s: cannot allocate op_data to release open handle for "
2427                       DFID"\n", ll_s2sbi(sb)->ll_fsname, PFID(&body->mbo_fid1));
2428
2429                 RETURN_EXIT;
2430         }
2431
2432         op_data->op_fid1 = body->mbo_fid1;
2433         op_data->op_open_handle = body->mbo_open_handle;
2434         op_data->op_mod_time = ktime_get_real_seconds();
2435         md_close(exp, op_data, NULL, &close_req);
2436         ptlrpc_req_finished(close_req);
2437         ll_finish_md_op_data(op_data);
2438
2439         EXIT;
2440 }
2441
2442 int ll_prep_inode(struct inode **inode, struct ptlrpc_request *req,
2443                   struct super_block *sb, struct lookup_intent *it)
2444 {
2445         struct ll_sb_info *sbi = NULL;
2446         struct lustre_md md = { NULL };
2447         int rc;
2448         ENTRY;
2449
2450         LASSERT(*inode || sb);
2451         sbi = sb ? ll_s2sbi(sb) : ll_i2sbi(*inode);
2452         rc = md_get_lustre_md(sbi->ll_md_exp, req, sbi->ll_dt_exp,
2453                               sbi->ll_md_exp, &md);
2454         if (rc != 0)
2455                 GOTO(out, rc);
2456
2457         if (*inode) {
2458                 rc = ll_update_inode(*inode, &md);
2459                 if (rc != 0)
2460                         GOTO(out, rc);
2461         } else {
2462                 LASSERT(sb != NULL);
2463
2464                 /*
2465                  * At this point server returns to client's same fid as client
2466                  * generated for creating. So using ->fid1 is okay here.
2467                  */
2468                 if (!fid_is_sane(&md.body->mbo_fid1)) {
2469                         CERROR("%s: Fid is insane "DFID"\n",
2470                                 sbi->ll_fsname,
2471                                 PFID(&md.body->mbo_fid1));
2472                         GOTO(out, rc = -EINVAL);
2473                 }
2474
2475                 *inode = ll_iget(sb, cl_fid_build_ino(&md.body->mbo_fid1,
2476                                              sbi->ll_flags & LL_SBI_32BIT_API),
2477                                  &md);
2478                 if (IS_ERR(*inode)) {
2479 #ifdef CONFIG_FS_POSIX_ACL
2480                         if (md.posix_acl) {
2481                                 posix_acl_release(md.posix_acl);
2482                                 md.posix_acl = NULL;
2483                         }
2484 #endif
2485                         rc = IS_ERR(*inode) ? PTR_ERR(*inode) : -ENOMEM;
2486                         *inode = NULL;
2487                         CERROR("new_inode -fatal: rc %d\n", rc);
2488                         GOTO(out, rc);
2489                 }
2490         }
2491
2492         /* Handling piggyback layout lock.
2493          * Layout lock can be piggybacked by getattr and open request.
2494          * The lsm can be applied to inode only if it comes with a layout lock
2495          * otherwise correct layout may be overwritten, for example:
2496          * 1. proc1: mdt returns a lsm but not granting layout
2497          * 2. layout was changed by another client
2498          * 3. proc2: refresh layout and layout lock granted
2499          * 4. proc1: to apply a stale layout */
2500         if (it != NULL && it->it_lock_mode != 0) {
2501                 struct lustre_handle lockh;
2502                 struct ldlm_lock *lock;
2503
2504                 lockh.cookie = it->it_lock_handle;
2505                 lock = ldlm_handle2lock(&lockh);
2506                 LASSERT(lock != NULL);
2507                 if (ldlm_has_layout(lock)) {
2508                         struct cl_object_conf conf;
2509
2510                         memset(&conf, 0, sizeof(conf));
2511                         conf.coc_opc = OBJECT_CONF_SET;
2512                         conf.coc_inode = *inode;
2513                         conf.coc_lock = lock;
2514                         conf.u.coc_layout = md.layout;
2515                         (void)ll_layout_conf(*inode, &conf);
2516                 }
2517                 LDLM_LOCK_PUT(lock);
2518         }
2519
2520         GOTO(out, rc = 0);
2521
2522 out:
2523         /* cleanup will be done if necessary */
2524         md_free_lustre_md(sbi->ll_md_exp, &md);
2525
2526         if (rc != 0 && it != NULL && it->it_op & IT_OPEN)
2527                 ll_open_cleanup(sb != NULL ? sb : (*inode)->i_sb, req);
2528
2529         return rc;
2530 }
2531
2532 int ll_obd_statfs(struct inode *inode, void __user *arg)
2533 {
2534         struct ll_sb_info *sbi = NULL;
2535         struct obd_export *exp;
2536         char *buf = NULL;
2537         struct obd_ioctl_data *data = NULL;
2538         __u32 type;
2539         int len = 0, rc;
2540
2541         if (!inode || !(sbi = ll_i2sbi(inode)))
2542                 GOTO(out_statfs, rc = -EINVAL);
2543
2544         rc = obd_ioctl_getdata(&buf, &len, arg);
2545         if (rc)
2546                 GOTO(out_statfs, rc);
2547
2548         data = (void*)buf;
2549         if (!data->ioc_inlbuf1 || !data->ioc_inlbuf2 ||
2550             !data->ioc_pbuf1 || !data->ioc_pbuf2)
2551                 GOTO(out_statfs, rc = -EINVAL);
2552
2553         if (data->ioc_inllen1 != sizeof(__u32) ||
2554             data->ioc_inllen2 != sizeof(__u32) ||
2555             data->ioc_plen1 != sizeof(struct obd_statfs) ||
2556             data->ioc_plen2 != sizeof(struct obd_uuid))
2557                 GOTO(out_statfs, rc = -EINVAL);
2558
2559         memcpy(&type, data->ioc_inlbuf1, sizeof(__u32));
2560         if (type & LL_STATFS_LMV)
2561                 exp = sbi->ll_md_exp;
2562         else if (type & LL_STATFS_LOV)
2563                 exp = sbi->ll_dt_exp;
2564         else
2565                 GOTO(out_statfs, rc = -ENODEV);
2566
2567         rc = obd_iocontrol(IOC_OBD_STATFS, exp, len, buf, NULL);
2568         if (rc)
2569                 GOTO(out_statfs, rc);
2570 out_statfs:
2571         OBD_FREE_LARGE(buf, len);
2572         return rc;
2573 }
2574
2575 /*
2576  * this is normally called in ll_fini_md_op_data(), but sometimes it needs to
2577  * be called early to avoid deadlock.
2578  */
2579 void ll_unlock_md_op_lsm(struct md_op_data *op_data)
2580 {
2581         if (op_data->op_mea2_sem) {
2582                 up_read(op_data->op_mea2_sem);
2583                 op_data->op_mea2_sem = NULL;
2584         }
2585
2586         if (op_data->op_mea1_sem) {
2587                 up_read(op_data->op_mea1_sem);
2588                 op_data->op_mea1_sem = NULL;
2589         }
2590 }
2591
2592 /* this function prepares md_op_data hint for passing it down to MD stack. */
2593 struct md_op_data *ll_prep_md_op_data(struct md_op_data *op_data,
2594                                       struct inode *i1, struct inode *i2,
2595                                       const char *name, size_t namelen,
2596                                       __u32 mode, __u32 opc, void *data)
2597 {
2598         LASSERT(i1 != NULL);
2599
2600         if (name == NULL) {
2601                 /* Do not reuse namelen for something else. */
2602                 if (namelen != 0)
2603                         return ERR_PTR(-EINVAL);
2604         } else {
2605                 if (namelen > ll_i2sbi(i1)->ll_namelen)
2606                         return ERR_PTR(-ENAMETOOLONG);
2607
2608                 if (!lu_name_is_valid_2(name, namelen))
2609                         return ERR_PTR(-EINVAL);
2610         }
2611
2612         if (op_data == NULL)
2613                 OBD_ALLOC_PTR(op_data);
2614
2615         if (op_data == NULL)
2616                 return ERR_PTR(-ENOMEM);
2617
2618         ll_i2gids(op_data->op_suppgids, i1, i2);
2619         op_data->op_fid1 = *ll_inode2fid(i1);
2620         op_data->op_default_stripe_offset = -1;
2621
2622         if (S_ISDIR(i1->i_mode)) {
2623                 down_read(&ll_i2info(i1)->lli_lsm_sem);
2624                 op_data->op_mea1_sem = &ll_i2info(i1)->lli_lsm_sem;
2625                 op_data->op_mea1 = ll_i2info(i1)->lli_lsm_md;
2626                 if (opc == LUSTRE_OPC_MKDIR)
2627                         op_data->op_default_stripe_offset =
2628                                    ll_i2info(i1)->lli_def_stripe_offset;
2629         }
2630
2631         if (i2) {
2632                 op_data->op_fid2 = *ll_inode2fid(i2);
2633                 if (S_ISDIR(i2->i_mode)) {
2634                         if (i2 != i1) {
2635                                 down_read(&ll_i2info(i2)->lli_lsm_sem);
2636                                 op_data->op_mea2_sem =
2637                                                 &ll_i2info(i2)->lli_lsm_sem;
2638                         }
2639                         op_data->op_mea2 = ll_i2info(i2)->lli_lsm_md;
2640                 }
2641         } else {
2642                 fid_zero(&op_data->op_fid2);
2643         }
2644
2645         if (ll_i2sbi(i1)->ll_flags & LL_SBI_64BIT_HASH)
2646                 op_data->op_cli_flags |= CLI_HASH64;
2647
2648         if (ll_need_32bit_api(ll_i2sbi(i1)))
2649                 op_data->op_cli_flags |= CLI_API32;
2650
2651         op_data->op_name = name;
2652         op_data->op_namelen = namelen;
2653         op_data->op_mode = mode;
2654         op_data->op_mod_time = ktime_get_real_seconds();
2655         op_data->op_fsuid = from_kuid(&init_user_ns, current_fsuid());
2656         op_data->op_fsgid = from_kgid(&init_user_ns, current_fsgid());
2657         op_data->op_cap = cfs_curproc_cap_pack();
2658         op_data->op_mds = 0;
2659         if ((opc == LUSTRE_OPC_CREATE) && (name != NULL) &&
2660              filename_is_volatile(name, namelen, &op_data->op_mds)) {
2661                 op_data->op_bias |= MDS_CREATE_VOLATILE;
2662         }
2663         op_data->op_data = data;
2664
2665         return op_data;
2666 }
2667
2668 void ll_finish_md_op_data(struct md_op_data *op_data)
2669 {
2670         ll_unlock_md_op_lsm(op_data);
2671         security_release_secctx(op_data->op_file_secctx,
2672                                 op_data->op_file_secctx_size);
2673         OBD_FREE_PTR(op_data);
2674 }
2675
2676 #ifdef HAVE_SUPEROPS_USE_DENTRY
2677 int ll_show_options(struct seq_file *seq, struct dentry *dentry)
2678 #else
2679 int ll_show_options(struct seq_file *seq, struct vfsmount *vfs)
2680 #endif
2681 {
2682         struct ll_sb_info *sbi;
2683
2684 #ifdef HAVE_SUPEROPS_USE_DENTRY
2685         LASSERT((seq != NULL) && (dentry != NULL));
2686         sbi = ll_s2sbi(dentry->d_sb);
2687 #else
2688         LASSERT((seq != NULL) && (vfs != NULL));
2689         sbi = ll_s2sbi(vfs->mnt_sb);
2690 #endif
2691
2692         if (sbi->ll_flags & LL_SBI_NOLCK)
2693                 seq_puts(seq, ",nolock");
2694
2695         if (sbi->ll_flags & LL_SBI_FLOCK)
2696                 seq_puts(seq, ",flock");
2697
2698         if (sbi->ll_flags & LL_SBI_LOCALFLOCK)
2699                 seq_puts(seq, ",localflock");
2700
2701         if (sbi->ll_flags & LL_SBI_USER_XATTR)
2702                 seq_puts(seq, ",user_xattr");
2703
2704         if (sbi->ll_flags & LL_SBI_LAZYSTATFS)
2705                 seq_puts(seq, ",lazystatfs");
2706
2707         if (sbi->ll_flags & LL_SBI_USER_FID2PATH)
2708                 seq_puts(seq, ",user_fid2path");
2709
2710         if (sbi->ll_flags & LL_SBI_ALWAYS_PING)
2711                 seq_puts(seq, ",always_ping");
2712
2713         RETURN(0);
2714 }
2715
2716 /**
2717  * Get obd name by cmd, and copy out to user space
2718  */
2719 int ll_get_obd_name(struct inode *inode, unsigned int cmd, unsigned long arg)
2720 {
2721         struct ll_sb_info *sbi = ll_i2sbi(inode);
2722         struct obd_device *obd;
2723         ENTRY;
2724
2725         if (cmd == OBD_IOC_GETDTNAME)
2726                 obd = class_exp2obd(sbi->ll_dt_exp);
2727         else if (cmd == OBD_IOC_GETMDNAME)
2728                 obd = class_exp2obd(sbi->ll_md_exp);
2729         else
2730                 RETURN(-EINVAL);
2731
2732         if (!obd)
2733                 RETURN(-ENOENT);
2734
2735         if (copy_to_user((void __user *)arg, obd->obd_name,
2736                          strlen(obd->obd_name) + 1))
2737                 RETURN(-EFAULT);
2738
2739         RETURN(0);
2740 }
2741
2742 static char* ll_d_path(struct dentry *dentry, char *buf, int bufsize)
2743 {
2744         char *path = NULL;
2745
2746         struct path p;
2747
2748         p.dentry = dentry;
2749         p.mnt = current->fs->root.mnt;
2750         path_get(&p);
2751         path = d_path(&p, buf, bufsize);
2752         path_put(&p);
2753         return path;
2754 }
2755
2756 void ll_dirty_page_discard_warn(struct page *page, int ioret)
2757 {
2758         char *buf, *path = NULL;
2759         struct dentry *dentry = NULL;
2760         struct inode *inode = page->mapping->host;
2761
2762         /* this can be called inside spin lock so use GFP_ATOMIC. */
2763         buf = (char *)__get_free_page(GFP_ATOMIC);
2764         if (buf != NULL) {
2765                 dentry = d_find_alias(page->mapping->host);
2766                 if (dentry != NULL)
2767                         path = ll_d_path(dentry, buf, PAGE_SIZE);
2768         }
2769
2770         CDEBUG(D_WARNING,
2771                "%s: dirty page discard: %s/fid: "DFID"/%s may get corrupted "
2772                "(rc %d)\n", ll_i2sbi(inode)->ll_fsname,
2773                s2lsi(page->mapping->host->i_sb)->lsi_lmd->lmd_dev,
2774                PFID(ll_inode2fid(inode)),
2775                (path && !IS_ERR(path)) ? path : "", ioret);
2776
2777         if (dentry != NULL)
2778                 dput(dentry);
2779
2780         if (buf != NULL)
2781                 free_page((unsigned long)buf);
2782 }
2783
2784 ssize_t ll_copy_user_md(const struct lov_user_md __user *md,
2785                         struct lov_user_md **kbuf)
2786 {
2787         struct lov_user_md      lum;
2788         ssize_t                 lum_size;
2789         ENTRY;
2790
2791         if (copy_from_user(&lum, md, sizeof(lum)))
2792                 RETURN(-EFAULT);
2793
2794         lum_size = ll_lov_user_md_size(&lum);
2795         if (lum_size < 0)
2796                 RETURN(lum_size);
2797
2798         OBD_ALLOC(*kbuf, lum_size);
2799         if (*kbuf == NULL)
2800                 RETURN(-ENOMEM);
2801
2802         if (copy_from_user(*kbuf, md, lum_size) != 0) {
2803                 OBD_FREE(*kbuf, lum_size);
2804                 RETURN(-EFAULT);
2805         }
2806
2807         RETURN(lum_size);
2808 }
2809
2810 /*
2811  * Compute llite root squash state after a change of root squash
2812  * configuration setting or add/remove of a lnet nid
2813  */
2814 void ll_compute_rootsquash_state(struct ll_sb_info *sbi)
2815 {
2816         struct root_squash_info *squash = &sbi->ll_squash;
2817         int i;
2818         bool matched;
2819         struct lnet_process_id id;
2820
2821         /* Update norootsquash flag */
2822         down_write(&squash->rsi_sem);
2823         if (list_empty(&squash->rsi_nosquash_nids))
2824                 sbi->ll_flags &= ~LL_SBI_NOROOTSQUASH;
2825         else {
2826                 /* Do not apply root squash as soon as one of our NIDs is
2827                  * in the nosquash_nids list */
2828                 matched = false;
2829                 i = 0;
2830                 while (LNetGetId(i++, &id) != -ENOENT) {
2831                         if (LNET_NETTYP(LNET_NIDNET(id.nid)) == LOLND)
2832                                 continue;
2833                         if (cfs_match_nid(id.nid, &squash->rsi_nosquash_nids)) {
2834                                 matched = true;
2835                                 break;
2836                         }
2837                 }
2838                 if (matched)
2839                         sbi->ll_flags |= LL_SBI_NOROOTSQUASH;
2840                 else
2841                         sbi->ll_flags &= ~LL_SBI_NOROOTSQUASH;
2842         }
2843         up_write(&squash->rsi_sem);
2844 }
2845
2846 /**
2847  * Parse linkea content to extract information about a given hardlink
2848  *
2849  * \param[in]   ldata      - Initialized linkea data
2850  * \param[in]   linkno     - Link identifier
2851  * \param[out]  parent_fid - The entry's parent FID
2852  * \param[out]  ln         - Entry name destination buffer
2853  *
2854  * \retval 0 on success
2855  * \retval Appropriate negative error code on failure
2856  */
2857 static int ll_linkea_decode(struct linkea_data *ldata, unsigned int linkno,
2858                             struct lu_fid *parent_fid, struct lu_name *ln)
2859 {
2860         unsigned int    idx;
2861         int             rc;
2862         ENTRY;
2863
2864         rc = linkea_init_with_rec(ldata);
2865         if (rc < 0)
2866                 RETURN(rc);
2867
2868         if (linkno >= ldata->ld_leh->leh_reccount)
2869                 /* beyond last link */
2870                 RETURN(-ENODATA);
2871
2872         linkea_first_entry(ldata);
2873         for (idx = 0; ldata->ld_lee != NULL; idx++) {
2874                 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen, ln,
2875                                     parent_fid);
2876                 if (idx == linkno)
2877                         break;
2878
2879                 linkea_next_entry(ldata);
2880         }
2881
2882         if (idx < linkno)
2883                 RETURN(-ENODATA);
2884
2885         RETURN(0);
2886 }
2887
2888 /**
2889  * Get parent FID and name of an identified link. Operation is performed for
2890  * a given link number, letting the caller iterate over linkno to list one or
2891  * all links of an entry.
2892  *
2893  * \param[in]     file - File descriptor against which to perform the operation
2894  * \param[in,out] arg  - User-filled structure containing the linkno to operate
2895  *                       on and the available size. It is eventually filled with
2896  *                       the requested information or left untouched on error
2897  *
2898  * \retval - 0 on success
2899  * \retval - Appropriate negative error code on failure
2900  */
2901 int ll_getparent(struct file *file, struct getparent __user *arg)
2902 {
2903         struct inode            *inode = file_inode(file);
2904         struct linkea_data      *ldata;
2905         struct lu_buf            buf = LU_BUF_NULL;
2906         struct lu_name           ln;
2907         struct lu_fid            parent_fid;
2908         __u32                    linkno;
2909         __u32                    name_size;
2910         int                      rc;
2911
2912         ENTRY;
2913
2914         if (!cfs_capable(CFS_CAP_DAC_READ_SEARCH) &&
2915             !(ll_i2sbi(inode)->ll_flags & LL_SBI_USER_FID2PATH))
2916                 RETURN(-EPERM);
2917
2918         if (get_user(name_size, &arg->gp_name_size))
2919                 RETURN(-EFAULT);
2920
2921         if (get_user(linkno, &arg->gp_linkno))
2922                 RETURN(-EFAULT);
2923
2924         if (name_size > PATH_MAX)
2925                 RETURN(-EINVAL);
2926
2927         OBD_ALLOC(ldata, sizeof(*ldata));
2928         if (ldata == NULL)
2929                 RETURN(-ENOMEM);
2930
2931         rc = linkea_data_new(ldata, &buf);
2932         if (rc < 0)
2933                 GOTO(ldata_free, rc);
2934
2935 #ifdef HAVE_XATTR_HANDLER_FLAGS
2936         rc = ll_xattr_list(inode, XATTR_NAME_LINK, XATTR_TRUSTED_T, buf.lb_buf,
2937                            buf.lb_len, OBD_MD_FLXATTR);
2938 #else
2939         rc = ll_getxattr(file_dentry(file), XATTR_NAME_LINK, buf.lb_buf,
2940                          buf.lb_len);
2941 #endif /* HAVE_XATTR_HANDLER_FLAGS */
2942         if (rc < 0)
2943                 GOTO(lb_free, rc);
2944
2945         rc = ll_linkea_decode(ldata, linkno, &parent_fid, &ln);
2946         if (rc < 0)
2947                 GOTO(lb_free, rc);
2948
2949         if (ln.ln_namelen >= name_size)
2950                 GOTO(lb_free, rc = -EOVERFLOW);
2951
2952         if (copy_to_user(&arg->gp_fid, &parent_fid, sizeof(arg->gp_fid)))
2953                 GOTO(lb_free, rc = -EFAULT);
2954
2955         if (copy_to_user(&arg->gp_name, ln.ln_name, ln.ln_namelen))
2956                 GOTO(lb_free, rc = -EFAULT);
2957
2958         if (put_user('\0', arg->gp_name + ln.ln_namelen))
2959                 GOTO(lb_free, rc = -EFAULT);
2960
2961 lb_free:
2962         lu_buf_free(&buf);
2963 ldata_free:
2964         OBD_FREE(ldata, sizeof(*ldata));
2965
2966         RETURN(rc);
2967 }