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