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