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