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