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