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