Whamcloud - gitweb
LU-15910 llite: enforce ROOT default on subdir mount
[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_sb_has_test_dummy_encryption(sb))
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_sb_has_test_dummy_encryption(sb))
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_sb_has_test_dummy_encryption(sb)) {
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=%s"},
941         {LL_SBI_TEST_DUMMY_ENCRYPTION,  "test_dummy_encryption"},
942         {LL_SBI_ENCRYPT,                "encrypt"},
943         {LL_SBI_ENCRYPT,                "noencrypt"},
944         {LL_SBI_FOREIGN_SYMLINK,        "foreign_symlink=%s"},
945         {LL_SBI_NUM_MOUNT_OPT,          NULL},
946
947         {LL_SBI_ACL,                    "acl"},
948         {LL_SBI_AGL_ENABLED,            "agl"},
949         {LL_SBI_64BIT_HASH,             "64bit_hash"},
950         {LL_SBI_LAYOUT_LOCK,            "layout"},
951         {LL_SBI_XATTR_CACHE,            "xattr_cache"},
952         {LL_SBI_NOROOTSQUASH,           "norootsquash"},
953         {LL_SBI_FAST_READ,              "fast_read"},
954         {LL_SBI_FILE_SECCTX,            "file_secctx"},
955         {LL_SBI_TINY_WRITE,             "tiny_write"},
956         {LL_SBI_FILE_HEAT,              "file_heat"},
957         {LL_SBI_PARALLEL_DIO,           "parallel_dio"},
958 };
959
960 int ll_sbi_flags_seq_show(struct seq_file *m, void *v)
961 {
962         struct super_block *sb = m->private;
963         int i;
964
965         for (i = 0; i < LL_SBI_NUM_FLAGS; i++) {
966                 int j;
967
968                 if (!test_bit(i, ll_s2sbi(sb)->ll_flags))
969                         continue;
970
971                 for (j = 0; j < ARRAY_SIZE(ll_sbi_flags_name); j++) {
972                         if (ll_sbi_flags_name[j].token == i &&
973                             ll_sbi_flags_name[j].pattern) {
974                                 seq_printf(m, "%s ",
975                                            ll_sbi_flags_name[j].pattern);
976                                 break;
977                         }
978                 }
979         }
980         seq_puts(m, "\b\n");
981         return 0;
982 }
983
984 /* non-client-specific mount options are parsed in lmd_parse */
985 static int ll_options(char *options, struct super_block *sb)
986 {
987         struct ll_sb_info *sbi = ll_s2sbi(sb);
988         char *s2, *s1, *opts;
989         int err = 0;
990
991         ENTRY;
992         if (!options)
993                 RETURN(0);
994
995         /* Don't stomp on lmd_opts */
996         opts = kstrdup(options, GFP_KERNEL);
997         if (!opts)
998                 RETURN(-ENOMEM);
999         s1 = opts;
1000         s2 = opts;
1001
1002         CDEBUG(D_CONFIG, "Parsing opts %s\n", options);
1003
1004         while ((s1 = strsep(&opts, ",")) != NULL) {
1005                 substring_t args[MAX_OPT_ARGS];
1006                 bool turn_off = false;
1007                 int token;
1008
1009                 if (!*s1)
1010                         continue;
1011
1012                 CDEBUG(D_SUPER, "next opt=%s\n", s1);
1013
1014                 if (strncmp(s1, "no", 2) == 0)
1015                         turn_off = true;
1016
1017                 /*
1018                  * Initialize args struct so we know whether arg was
1019                  * found; some options take optional arguments.
1020                  */
1021                 args[0].to = NULL;
1022                 args[0].from = NULL;
1023                 token = match_token(s1, ll_sbi_flags_name, args);
1024                 if (token == LL_SBI_NUM_MOUNT_OPT) {
1025                         if (match_wildcard("context", s1) ||
1026                             match_wildcard("fscontext", s1) ||
1027                             match_wildcard("defcontext", s1) ||
1028                             match_wildcard("rootcontext",s1))
1029                                 continue;
1030
1031                         LCONSOLE_ERROR_MSG(0x152,
1032                                            "Unknown option '%s', won't mount.\n",
1033                                            s1);
1034                         RETURN(-EINVAL);
1035                 }
1036
1037                 switch (token) {
1038                 case LL_SBI_NOLCK:
1039                 case LL_SBI_32BIT_API:
1040                 case LL_SBI_64BIT_HASH:
1041                 case LL_SBI_ALWAYS_PING:
1042                         set_bit(token, sbi->ll_flags);
1043                         break;
1044
1045                 case LL_SBI_FLOCK:
1046                         clear_bit(LL_SBI_LOCALFLOCK, sbi->ll_flags);
1047                         if (turn_off)
1048                                 clear_bit(LL_SBI_FLOCK, sbi->ll_flags);
1049                         else
1050                                 set_bit(token, sbi->ll_flags);
1051                         break;
1052
1053                 case LL_SBI_LOCALFLOCK:
1054                         clear_bit(LL_SBI_FLOCK, sbi->ll_flags);
1055                         set_bit(token, sbi->ll_flags);
1056                         break;
1057
1058                 case LL_SBI_CHECKSUM:
1059                         sbi->ll_checksum_set = 1;
1060                         fallthrough;
1061                 case LL_SBI_USER_XATTR:
1062                 case LL_SBI_USER_FID2PATH:
1063                 case LL_SBI_LRU_RESIZE:
1064                 case LL_SBI_LAZYSTATFS:
1065                 case LL_SBI_VERBOSE:
1066                         if (turn_off)
1067                                 clear_bit(token, sbi->ll_flags);
1068                         else
1069                                 set_bit(token, sbi->ll_flags);
1070                         break;
1071                 case LL_SBI_TEST_DUMMY_ENCRYPTION: {
1072 #ifdef HAVE_LUSTRE_CRYPTO
1073 #ifdef HAVE_FSCRYPT_DUMMY_CONTEXT_ENABLED
1074                         set_bit(token, sbi->ll_flags);
1075 #else
1076                         struct lustre_sb_info *lsi = s2lsi(sb);
1077
1078                         err = llcrypt_set_test_dummy_encryption(sb, &args[0],
1079                                                                 &lsi->lsi_dummy_enc_ctx);
1080                         if (!err)
1081                                 break;
1082
1083                         if (err == -EEXIST)
1084                                 LCONSOLE_WARN(
1085                                          "Can't change test_dummy_encryption");
1086                         else if (err == -EINVAL)
1087                                 LCONSOLE_WARN(
1088                                         "Value of option \"%s\" unrecognized",
1089                                         options);
1090                         else
1091                                 LCONSOLE_WARN(
1092                                          "Error processing option \"%s\" [%d]",
1093                                          options, err);
1094                         err = -1;
1095 #endif
1096 #else
1097                         LCONSOLE_WARN("Test dummy encryption mount option ignored: encryption not supported\n");
1098 #endif
1099                         break;
1100                 }
1101                 case LL_SBI_ENCRYPT:
1102 #ifdef HAVE_LUSTRE_CRYPTO
1103                         if (turn_off)
1104                                 clear_bit(token, sbi->ll_flags);
1105                         else
1106                                 set_bit(token, sbi->ll_flags);
1107 #else
1108                         LCONSOLE_WARN("noencrypt or encrypt mount option ignored: encryption not supported\n");
1109 #endif
1110                         break;
1111                 case LL_SBI_FOREIGN_SYMLINK:
1112                         /* non-default prefix provided ? */
1113                         if (args->from) {
1114                                 size_t old_len;
1115                                 char *old;
1116
1117                                 /* path must be absolute */
1118                                 if (args->from[0] != '/') {
1119                                         LCONSOLE_ERROR_MSG(0x152,
1120                                                            "foreign prefix '%s' must be an absolute path\n",
1121                                                            args->from);
1122                                         RETURN(-EINVAL);
1123                                 }
1124
1125                                 old_len = sbi->ll_foreign_symlink_prefix_size;
1126                                 old = sbi->ll_foreign_symlink_prefix;
1127                                 /* alloc for path length and '\0' */
1128                                 sbi->ll_foreign_symlink_prefix = match_strdup(args);
1129                                 if (!sbi->ll_foreign_symlink_prefix) {
1130                                         /* restore previous */
1131                                         sbi->ll_foreign_symlink_prefix = old;
1132                                         sbi->ll_foreign_symlink_prefix_size =
1133                                                 old_len;
1134                                         RETURN(-ENOMEM);
1135                                 }
1136                                 sbi->ll_foreign_symlink_prefix_size =
1137                                         args->to - args->from + 1;
1138                                 OBD_ALLOC_POST(sbi->ll_foreign_symlink_prefix,
1139                                                sbi->ll_foreign_symlink_prefix_size,
1140                                                "kmalloced");
1141                                 if (old)
1142                                         OBD_FREE(old, old_len);
1143
1144                                 /* enable foreign symlink support */
1145                                 set_bit(token, sbi->ll_flags);
1146                         } else {
1147                                 LCONSOLE_ERROR_MSG(0x152,
1148                                                    "invalid %s option\n", s1);
1149                         }
1150                 fallthrough;
1151                 default:
1152                         break;
1153                 }
1154         }
1155         kfree(opts);
1156         RETURN(err);
1157 }
1158
1159 void ll_lli_init(struct ll_inode_info *lli)
1160 {
1161         lli->lli_inode_magic = LLI_INODE_MAGIC;
1162         lli->lli_flags = 0;
1163         rwlock_init(&lli->lli_lock);
1164         lli->lli_posix_acl = NULL;
1165         /* Do not set lli_fid, it has been initialized already. */
1166         fid_zero(&lli->lli_pfid);
1167         lli->lli_mds_read_och = NULL;
1168         lli->lli_mds_write_och = NULL;
1169         lli->lli_mds_exec_och = NULL;
1170         lli->lli_open_fd_read_count = 0;
1171         lli->lli_open_fd_write_count = 0;
1172         lli->lli_open_fd_exec_count = 0;
1173         mutex_init(&lli->lli_och_mutex);
1174         spin_lock_init(&lli->lli_agl_lock);
1175         spin_lock_init(&lli->lli_layout_lock);
1176         ll_layout_version_set(lli, CL_LAYOUT_GEN_NONE);
1177         lli->lli_clob = NULL;
1178
1179         init_rwsem(&lli->lli_xattrs_list_rwsem);
1180         mutex_init(&lli->lli_xattrs_enq_lock);
1181
1182         LASSERT(lli->lli_vfs_inode.i_mode != 0);
1183         if (S_ISDIR(lli->lli_vfs_inode.i_mode)) {
1184                 lli->lli_opendir_key = NULL;
1185                 lli->lli_sai = NULL;
1186                 spin_lock_init(&lli->lli_sa_lock);
1187                 lli->lli_opendir_pid = 0;
1188                 lli->lli_sa_enabled = 0;
1189                 init_rwsem(&lli->lli_lsm_sem);
1190         } else {
1191                 mutex_init(&lli->lli_size_mutex);
1192                 mutex_init(&lli->lli_setattr_mutex);
1193                 lli->lli_symlink_name = NULL;
1194                 ll_trunc_sem_init(&lli->lli_trunc_sem);
1195                 range_lock_tree_init(&lli->lli_write_tree);
1196                 init_rwsem(&lli->lli_glimpse_sem);
1197                 lli->lli_glimpse_time = ktime_set(0, 0);
1198                 INIT_LIST_HEAD(&lli->lli_agl_list);
1199                 lli->lli_agl_index = 0;
1200                 lli->lli_async_rc = 0;
1201                 spin_lock_init(&lli->lli_heat_lock);
1202                 obd_heat_clear(lli->lli_heat_instances, OBD_HEAT_COUNT);
1203                 lli->lli_heat_flags = 0;
1204                 mutex_init(&lli->lli_pcc_lock);
1205                 lli->lli_pcc_state = PCC_STATE_FL_NONE;
1206                 lli->lli_pcc_inode = NULL;
1207                 lli->lli_pcc_dsflags = PCC_DATASET_INVALID;
1208                 lli->lli_pcc_generation = 0;
1209                 mutex_init(&lli->lli_group_mutex);
1210                 lli->lli_group_users = 0;
1211                 lli->lli_group_gid = 0;
1212         }
1213         mutex_init(&lli->lli_layout_mutex);
1214         memset(lli->lli_jobid, 0, sizeof(lli->lli_jobid));
1215         /* ll_cl_context initialize */
1216         INIT_LIST_HEAD(&lli->lli_lccs);
1217 }
1218
1219 #define MAX_STRING_SIZE 128
1220
1221 #ifndef HAVE_SUPER_SETUP_BDI_NAME
1222 #ifndef HAVE_BDI_CAP_MAP_COPY
1223 # define BDI_CAP_MAP_COPY       0
1224 #endif
1225
1226 static int super_setup_bdi_name(struct super_block *sb, char *fmt, ...)
1227 {
1228         struct  lustre_sb_info *lsi = s2lsi(sb);
1229         char buf[MAX_STRING_SIZE];
1230         va_list args;
1231         int err;
1232
1233         err = bdi_init(&lsi->lsi_bdi);
1234         if (err)
1235                 return err;
1236
1237         lsi->lsi_flags |= LSI_BDI_INITIALIZED;
1238         lsi->lsi_bdi.capabilities = BDI_CAP_MAP_COPY;
1239         lsi->lsi_bdi.name = "lustre";
1240         va_start(args, fmt);
1241         vsnprintf(buf, MAX_STRING_SIZE, fmt, args);
1242         va_end(args);
1243         err = bdi_register(&lsi->lsi_bdi, NULL, "%s", buf);
1244         va_end(args);
1245         if (!err)
1246                 sb->s_bdi = &lsi->lsi_bdi;
1247
1248         return err;
1249 }
1250 #endif /* !HAVE_SUPER_SETUP_BDI_NAME */
1251
1252 int ll_fill_super(struct super_block *sb)
1253 {
1254         struct  lustre_profile *lprof = NULL;
1255         struct  lustre_sb_info *lsi = s2lsi(sb);
1256         struct  ll_sb_info *sbi = NULL;
1257         char    *dt = NULL, *md = NULL;
1258         char    *profilenm = get_profile_name(sb);
1259         struct config_llog_instance *cfg;
1260         /* %p for void* in printf needs 16+2 characters: 0xffffffffffffffff */
1261         const int instlen = LUSTRE_MAXINSTANCE + 2;
1262         unsigned long cfg_instance = ll_get_cfg_instance(sb);
1263         char name[MAX_STRING_SIZE];
1264         int md_len = 0;
1265         int dt_len = 0;
1266         uuid_t uuid;
1267         char *ptr;
1268         int len;
1269         int err;
1270
1271         ENTRY;
1272         /* for ASLR, to map between cfg_instance and hashed ptr */
1273         CDEBUG(D_VFSTRACE, "VFS Op: cfg_instance %s-%016lx (sb %p)\n",
1274                profilenm, cfg_instance, sb);
1275
1276         OBD_RACE(OBD_FAIL_LLITE_RACE_MOUNT);
1277
1278         OBD_ALLOC_PTR(cfg);
1279         if (cfg == NULL)
1280                 GOTO(out_free_cfg, err = -ENOMEM);
1281
1282         /* client additional sb info */
1283         lsi->lsi_llsbi = sbi = ll_init_sbi();
1284         if (IS_ERR(sbi))
1285                 GOTO(out_free_cfg, err = PTR_ERR(sbi));
1286
1287         err = ll_options(lsi->lsi_lmd->lmd_opts, sb);
1288         if (err)
1289                 GOTO(out_free_cfg, err);
1290
1291         /* kernel >= 2.6.38 store dentry operations in sb->s_d_op. */
1292         sb->s_d_op = &ll_d_ops;
1293
1294         /* UUID handling */
1295         generate_random_uuid(uuid.b);
1296         snprintf(sbi->ll_sb_uuid.uuid, sizeof(sbi->ll_sb_uuid), "%pU", uuid.b);
1297
1298         CDEBUG(D_CONFIG, "llite sb uuid: %s\n", sbi->ll_sb_uuid.uuid);
1299
1300         /* Get fsname */
1301         len = strlen(profilenm);
1302         ptr = strrchr(profilenm, '-');
1303         if (ptr && (strcmp(ptr, "-client") == 0))
1304                 len -= 7;
1305
1306         if (len > LUSTRE_MAXFSNAME) {
1307                 if (unlikely(len >= MAX_STRING_SIZE))
1308                         len = MAX_STRING_SIZE - 1;
1309                 strncpy(name, profilenm, len);
1310                 name[len] = '\0';
1311                 err = -ENAMETOOLONG;
1312                 CERROR("%s: fsname longer than %u characters: rc = %d\n",
1313                        name, LUSTRE_MAXFSNAME, err);
1314                 GOTO(out_free_cfg, err);
1315         }
1316         strncpy(sbi->ll_fsname, profilenm, len);
1317         sbi->ll_fsname[len] = '\0';
1318
1319         /* Mount info */
1320         snprintf(name, sizeof(name), "%.*s-%016lx", len,
1321                  profilenm, cfg_instance);
1322
1323         err = super_setup_bdi_name(sb, "%s", name);
1324         if (err)
1325                 GOTO(out_free_cfg, err);
1326
1327         /* disable kernel readahead */
1328         sb->s_bdi->ra_pages = 0;
1329
1330         /* Call ll_debugfs_register_super() before lustre_process_log()
1331          * so that "llite.*.*" params can be processed correctly.
1332          */
1333         err = ll_debugfs_register_super(sb, name);
1334         if (err < 0) {
1335                 CERROR("%s: could not register mountpoint in llite: rc = %d\n",
1336                        sbi->ll_fsname, err);
1337                 err = 0;
1338         }
1339
1340         /* The cfg_instance is a value unique to this super, in case some
1341          * joker tries to mount the same fs at two mount points.
1342          */
1343         cfg->cfg_instance = cfg_instance;
1344         cfg->cfg_uuid = lsi->lsi_llsbi->ll_sb_uuid;
1345         cfg->cfg_callback = class_config_llog_handler;
1346         cfg->cfg_sub_clds = CONFIG_SUB_CLIENT;
1347         /* set up client obds */
1348         err = lustre_process_log(sb, profilenm, cfg);
1349         if (err < 0)
1350                 GOTO(out_debugfs, err);
1351
1352         /* Profile set with LCFG_MOUNTOPT so we can find our mdc and osc obds */
1353         lprof = class_get_profile(profilenm);
1354         if (lprof == NULL) {
1355                 LCONSOLE_ERROR_MSG(0x156, "The client profile '%s' could not be"
1356                                    " read from the MGS.  Does that filesystem "
1357                                    "exist?\n", profilenm);
1358                 GOTO(out_debugfs, err = -EINVAL);
1359         }
1360         CDEBUG(D_CONFIG, "Found profile %s: mdc=%s osc=%s\n", profilenm,
1361                lprof->lp_md, lprof->lp_dt);
1362
1363         dt_len = strlen(lprof->lp_dt) + instlen + 2;
1364         OBD_ALLOC(dt, dt_len);
1365         if (!dt)
1366                 GOTO(out_profile, err = -ENOMEM);
1367         snprintf(dt, dt_len - 1, "%s-%016lx", lprof->lp_dt, cfg_instance);
1368
1369         md_len = strlen(lprof->lp_md) + instlen + 2;
1370         OBD_ALLOC(md, md_len);
1371         if (!md)
1372                 GOTO(out_free_dt, err = -ENOMEM);
1373         snprintf(md, md_len - 1, "%s-%016lx", lprof->lp_md, cfg_instance);
1374
1375         /* connections, registrations, sb setup */
1376         err = client_common_fill_super(sb, md, dt);
1377         if (err < 0)
1378                 GOTO(out_free_md, err);
1379
1380         sbi->ll_client_common_fill_super_succeeded = 1;
1381
1382 out_free_md:
1383         if (md)
1384                 OBD_FREE(md, md_len);
1385 out_free_dt:
1386         if (dt)
1387                 OBD_FREE(dt, dt_len);
1388 out_profile:
1389         if (lprof)
1390                 class_put_profile(lprof);
1391 out_debugfs:
1392         if (err < 0)
1393                 ll_debugfs_unregister_super(sb);
1394 out_free_cfg:
1395         if (cfg)
1396                 OBD_FREE_PTR(cfg);
1397
1398         if (err)
1399                 ll_put_super(sb);
1400         else if (test_bit(LL_SBI_VERBOSE, sbi->ll_flags))
1401                 LCONSOLE_WARN("Mounted %s\n", profilenm);
1402         RETURN(err);
1403 } /* ll_fill_super */
1404
1405 void ll_put_super(struct super_block *sb)
1406 {
1407         struct config_llog_instance cfg, params_cfg;
1408         struct obd_device *obd;
1409         struct lustre_sb_info *lsi = s2lsi(sb);
1410         struct ll_sb_info *sbi = ll_s2sbi(sb);
1411         char *profilenm = get_profile_name(sb);
1412         unsigned long cfg_instance = ll_get_cfg_instance(sb);
1413         long ccc_count;
1414         int next, force = 1, rc = 0;
1415         ENTRY;
1416
1417         if (IS_ERR(sbi))
1418                 GOTO(out_no_sbi, 0);
1419
1420         /* Should replace instance_id with something better for ASLR */
1421         CDEBUG(D_VFSTRACE, "VFS Op: cfg_instance %s-%016lx (sb %p)\n",
1422                profilenm, cfg_instance, sb);
1423
1424         cfg.cfg_instance = cfg_instance;
1425         lustre_end_log(sb, profilenm, &cfg);
1426
1427         params_cfg.cfg_instance = cfg_instance;
1428         lustre_end_log(sb, PARAMS_FILENAME, &params_cfg);
1429
1430         if (sbi->ll_md_exp) {
1431                 obd = class_exp2obd(sbi->ll_md_exp);
1432                 if (obd)
1433                         force = obd->obd_force;
1434         }
1435
1436         /* Wait for unstable pages to be committed to stable storage */
1437         if (force == 0) {
1438                 rc = l_wait_event_abortable(
1439                         sbi->ll_cache->ccc_unstable_waitq,
1440                         atomic_long_read(&sbi->ll_cache->ccc_unstable_nr) == 0);
1441         }
1442
1443         ccc_count = atomic_long_read(&sbi->ll_cache->ccc_unstable_nr);
1444         if (force == 0 && rc != -ERESTARTSYS)
1445                 LASSERTF(ccc_count == 0, "count: %li\n", ccc_count);
1446
1447         /* We need to set force before the lov_disconnect in
1448          * lustre_common_put_super, since l_d cleans up osc's as well.
1449          */
1450         if (force) {
1451                 next = 0;
1452                 while ((obd = class_devices_in_group(&sbi->ll_sb_uuid,
1453                                                      &next)) != NULL) {
1454                         obd->obd_force = force;
1455                 }
1456         }
1457
1458         if (sbi->ll_client_common_fill_super_succeeded) {
1459                 /* Only if client_common_fill_super succeeded */
1460                 client_common_put_super(sb);
1461         }
1462
1463         next = 0;
1464         while ((obd = class_devices_in_group(&sbi->ll_sb_uuid, &next)))
1465                 class_manual_cleanup(obd);
1466
1467         if (test_bit(LL_SBI_VERBOSE, sbi->ll_flags))
1468                 LCONSOLE_WARN("Unmounted %s\n", profilenm ? profilenm : "");
1469
1470         if (profilenm)
1471                 class_del_profile(profilenm);
1472
1473 #ifndef HAVE_SUPER_SETUP_BDI_NAME
1474         if (lsi->lsi_flags & LSI_BDI_INITIALIZED) {
1475                 bdi_destroy(&lsi->lsi_bdi);
1476                 lsi->lsi_flags &= ~LSI_BDI_INITIALIZED;
1477         }
1478 #endif
1479
1480         llcrypt_free_dummy_context(&lsi->lsi_dummy_enc_ctx);
1481         ll_free_sbi(sb);
1482         lsi->lsi_llsbi = NULL;
1483 out_no_sbi:
1484         lustre_common_put_super(sb);
1485
1486         cl_env_cache_purge(~0);
1487
1488         EXIT;
1489 } /* client_put_super */
1490
1491 struct inode *ll_inode_from_resource_lock(struct ldlm_lock *lock)
1492 {
1493         struct inode *inode = NULL;
1494
1495         /* NOTE: we depend on atomic igrab() -bzzz */
1496         lock_res_and_lock(lock);
1497         if (lock->l_resource->lr_lvb_inode) {
1498                 struct ll_inode_info * lli;
1499                 lli = ll_i2info(lock->l_resource->lr_lvb_inode);
1500                 if (lli->lli_inode_magic == LLI_INODE_MAGIC) {
1501                         inode = igrab(lock->l_resource->lr_lvb_inode);
1502                 } else {
1503                         inode = lock->l_resource->lr_lvb_inode;
1504                         LDLM_DEBUG_LIMIT(inode->i_state & I_FREEING ?  D_INFO :
1505                                          D_WARNING, lock, "lr_lvb_inode %p is "
1506                                          "bogus: magic %08x",
1507                                          lock->l_resource->lr_lvb_inode,
1508                                          lli->lli_inode_magic);
1509                         inode = NULL;
1510                 }
1511         }
1512         unlock_res_and_lock(lock);
1513         return inode;
1514 }
1515
1516 void ll_dir_clear_lsm_md(struct inode *inode)
1517 {
1518         struct ll_inode_info *lli = ll_i2info(inode);
1519
1520         LASSERT(S_ISDIR(inode->i_mode));
1521
1522         if (lli->lli_lsm_md) {
1523                 lmv_free_memmd(lli->lli_lsm_md);
1524                 lli->lli_lsm_md = NULL;
1525         }
1526
1527         if (lli->lli_default_lsm_md) {
1528                 lmv_free_memmd(lli->lli_default_lsm_md);
1529                 lli->lli_default_lsm_md = NULL;
1530         }
1531 }
1532
1533 static struct inode *ll_iget_anon_dir(struct super_block *sb,
1534                                       const struct lu_fid *fid,
1535                                       struct lustre_md *md)
1536 {
1537         struct ll_sb_info *sbi = ll_s2sbi(sb);
1538         struct ll_inode_info *lli;
1539         struct mdt_body *body = md->body;
1540         struct inode *inode;
1541         ino_t ino;
1542
1543         ENTRY;
1544
1545         LASSERT(md->lmv);
1546         ino = cl_fid_build_ino(fid, test_bit(LL_SBI_32BIT_API, sbi->ll_flags));
1547         inode = iget_locked(sb, ino);
1548         if (inode == NULL) {
1549                 CERROR("%s: failed get simple inode "DFID": rc = -ENOENT\n",
1550                        sbi->ll_fsname, PFID(fid));
1551                 RETURN(ERR_PTR(-ENOENT));
1552         }
1553
1554         lli = ll_i2info(inode);
1555         if (inode->i_state & I_NEW) {
1556                 inode->i_mode = (inode->i_mode & ~S_IFMT) |
1557                                 (body->mbo_mode & S_IFMT);
1558                 LASSERTF(S_ISDIR(inode->i_mode), "Not slave inode "DFID"\n",
1559                          PFID(fid));
1560
1561                 inode->i_mtime.tv_sec = 0;
1562                 inode->i_atime.tv_sec = 0;
1563                 inode->i_ctime.tv_sec = 0;
1564                 inode->i_rdev = 0;
1565
1566 #ifdef HAVE_BACKING_DEV_INFO
1567                 /* initializing backing dev info. */
1568                 inode->i_mapping->backing_dev_info =
1569                                                 &s2lsi(inode->i_sb)->lsi_bdi;
1570 #endif
1571                 inode->i_op = &ll_dir_inode_operations;
1572                 inode->i_fop = &ll_dir_operations;
1573                 lli->lli_fid = *fid;
1574                 ll_lli_init(lli);
1575
1576                 /* master object FID */
1577                 lli->lli_pfid = body->mbo_fid1;
1578                 CDEBUG(D_INODE, "lli %p slave "DFID" master "DFID"\n",
1579                        lli, PFID(fid), PFID(&lli->lli_pfid));
1580                 unlock_new_inode(inode);
1581         } else {
1582                 /* in directory restripe/auto-split, a directory will be
1583                  * transformed to a stripe if it's plain, set its pfid here,
1584                  * otherwise ll_lock_cancel_bits() can't find the master inode.
1585                  */
1586                 lli->lli_pfid = body->mbo_fid1;
1587         }
1588
1589         RETURN(inode);
1590 }
1591
1592 static int ll_init_lsm_md(struct inode *inode, struct lustre_md *md)
1593 {
1594         struct lu_fid *fid;
1595         struct lmv_stripe_md *lsm = md->lmv;
1596         struct ll_inode_info *lli = ll_i2info(inode);
1597         int i;
1598
1599         LASSERT(lsm != NULL);
1600
1601         CDEBUG(D_INODE, "%s: "DFID" set dir layout:\n",
1602                ll_i2sbi(inode)->ll_fsname, PFID(&lli->lli_fid));
1603         lsm_md_dump(D_INODE, lsm);
1604
1605         if (!lmv_dir_striped(lsm))
1606                 goto out;
1607
1608         /* XXX sigh, this lsm_root initialization should be in
1609          * LMV layer, but it needs ll_iget right now, so we
1610          * put this here right now. */
1611         for (i = 0; i < lsm->lsm_md_stripe_count; i++) {
1612                 fid = &lsm->lsm_md_oinfo[i].lmo_fid;
1613                 LASSERT(lsm->lsm_md_oinfo[i].lmo_root == NULL);
1614
1615                 if (!fid_is_sane(fid))
1616                         continue;
1617
1618                 /* Unfortunately ll_iget will call ll_update_inode,
1619                  * where the initialization of slave inode is slightly
1620                  * different, so it reset lsm_md to NULL to avoid
1621                  * initializing lsm for slave inode. */
1622                 lsm->lsm_md_oinfo[i].lmo_root =
1623                                 ll_iget_anon_dir(inode->i_sb, fid, md);
1624                 if (IS_ERR(lsm->lsm_md_oinfo[i].lmo_root)) {
1625                         int rc = PTR_ERR(lsm->lsm_md_oinfo[i].lmo_root);
1626
1627                         lsm->lsm_md_oinfo[i].lmo_root = NULL;
1628                         while (i-- > 0) {
1629                                 iput(lsm->lsm_md_oinfo[i].lmo_root);
1630                                 lsm->lsm_md_oinfo[i].lmo_root = NULL;
1631                         }
1632                         return rc;
1633                 }
1634         }
1635 out:
1636         lli->lli_lsm_md = lsm;
1637
1638         return 0;
1639 }
1640
1641 static void ll_update_default_lsm_md(struct inode *inode, struct lustre_md *md)
1642 {
1643         struct ll_inode_info *lli = ll_i2info(inode);
1644
1645         ENTRY;
1646
1647         if (!md->default_lmv) {
1648                 /* clear default lsm */
1649                 if (lli->lli_default_lsm_md) {
1650                         down_write(&lli->lli_lsm_sem);
1651                         if (lli->lli_default_lsm_md) {
1652                                 lmv_free_memmd(lli->lli_default_lsm_md);
1653                                 lli->lli_default_lsm_md = NULL;
1654                         }
1655                         up_write(&lli->lli_lsm_sem);
1656                 }
1657                 RETURN_EXIT;
1658         }
1659
1660         if (lli->lli_default_lsm_md) {
1661                 /* do nonthing if default lsm isn't changed */
1662                 down_read(&lli->lli_lsm_sem);
1663                 if (lli->lli_default_lsm_md &&
1664                     lsm_md_eq(lli->lli_default_lsm_md, md->default_lmv)) {
1665                         up_read(&lli->lli_lsm_sem);
1666                         RETURN_EXIT;
1667                 }
1668                 up_read(&lli->lli_lsm_sem);
1669         }
1670
1671         down_write(&lli->lli_lsm_sem);
1672         if (lli->lli_default_lsm_md)
1673                 lmv_free_memmd(lli->lli_default_lsm_md);
1674         lli->lli_default_lsm_md = md->default_lmv;
1675         lsm_md_dump(D_INODE, md->default_lmv);
1676         md->default_lmv = NULL;
1677         up_write(&lli->lli_lsm_sem);
1678         RETURN_EXIT;
1679 }
1680
1681 static int ll_update_lsm_md(struct inode *inode, struct lustre_md *md)
1682 {
1683         struct ll_inode_info *lli = ll_i2info(inode);
1684         struct lmv_stripe_md *lsm = md->lmv;
1685         struct cl_attr  *attr;
1686         int rc = 0;
1687
1688         ENTRY;
1689
1690         LASSERT(S_ISDIR(inode->i_mode));
1691         CDEBUG(D_INODE, "update lsm %p of "DFID"\n", lli->lli_lsm_md,
1692                PFID(ll_inode2fid(inode)));
1693
1694         /* update default LMV */
1695         if (md->default_lmv)
1696                 ll_update_default_lsm_md(inode, md);
1697
1698         /* after dir migration/restripe, a stripe may be turned into a
1699          * directory, in this case, zero out its lli_pfid.
1700          */
1701         if (unlikely(fid_is_norm(&lli->lli_pfid)))
1702                 fid_zero(&lli->lli_pfid);
1703
1704         /*
1705          * no striped information from request, lustre_md from req does not
1706          * include stripeEA, see ll_md_setattr()
1707          */
1708         if (!lsm)
1709                 RETURN(0);
1710
1711         /*
1712          * normally dir layout doesn't change, only take read lock to check
1713          * that to avoid blocking other MD operations.
1714          */
1715         down_read(&lli->lli_lsm_sem);
1716
1717         /* some current lookup initialized lsm, and unchanged */
1718         if (lli->lli_lsm_md && lsm_md_eq(lli->lli_lsm_md, lsm))
1719                 GOTO(unlock, rc = 0);
1720
1721         /* if dir layout doesn't match, check whether version is increased,
1722          * which means layout is changed, this happens in dir split/merge and
1723          * lfsck.
1724          *
1725          * foreign LMV should not change.
1726          */
1727         if (lli->lli_lsm_md && lmv_dir_striped(lli->lli_lsm_md) &&
1728             lsm->lsm_md_layout_version <=
1729             lli->lli_lsm_md->lsm_md_layout_version) {
1730                 CERROR("%s: "DFID" dir layout mismatch:\n",
1731                        ll_i2sbi(inode)->ll_fsname, PFID(&lli->lli_fid));
1732                 lsm_md_dump(D_ERROR, lli->lli_lsm_md);
1733                 lsm_md_dump(D_ERROR, lsm);
1734                 GOTO(unlock, rc = -EINVAL);
1735         }
1736
1737         up_read(&lli->lli_lsm_sem);
1738         down_write(&lli->lli_lsm_sem);
1739         /* clear existing lsm */
1740         if (lli->lli_lsm_md) {
1741                 lmv_free_memmd(lli->lli_lsm_md);
1742                 lli->lli_lsm_md = NULL;
1743         }
1744
1745         rc = ll_init_lsm_md(inode, md);
1746         if (rc) {
1747                 up_write(&lli->lli_lsm_sem);
1748                 RETURN(rc);
1749         }
1750
1751         /* md_merge_attr() may take long, since lsm is already set, switch to
1752          * read lock.
1753          */
1754         downgrade_write(&lli->lli_lsm_sem);
1755
1756         /* set md->lmv to NULL, so the following free lustre_md will not free
1757          * this lsm.
1758          */
1759         md->lmv = NULL;
1760
1761         if (!lmv_dir_striped(lli->lli_lsm_md))
1762                 GOTO(unlock, rc = 0);
1763
1764         OBD_ALLOC_PTR(attr);
1765         if (!attr)
1766                 GOTO(unlock, rc = -ENOMEM);
1767
1768         /* validate the lsm */
1769         rc = md_merge_attr(ll_i2mdexp(inode), lli->lli_lsm_md, attr,
1770                            ll_md_blocking_ast);
1771         if (!rc) {
1772                 if (md->body->mbo_valid & OBD_MD_FLNLINK)
1773                         md->body->mbo_nlink = attr->cat_nlink;
1774                 if (md->body->mbo_valid & OBD_MD_FLSIZE)
1775                         md->body->mbo_size = attr->cat_size;
1776                 if (md->body->mbo_valid & OBD_MD_FLATIME)
1777                         md->body->mbo_atime = attr->cat_atime;
1778                 if (md->body->mbo_valid & OBD_MD_FLCTIME)
1779                         md->body->mbo_ctime = attr->cat_ctime;
1780                 if (md->body->mbo_valid & OBD_MD_FLMTIME)
1781                         md->body->mbo_mtime = attr->cat_mtime;
1782         }
1783
1784         OBD_FREE_PTR(attr);
1785         GOTO(unlock, rc);
1786 unlock:
1787         up_read(&lli->lli_lsm_sem);
1788
1789         return rc;
1790 }
1791
1792 void ll_clear_inode(struct inode *inode)
1793 {
1794         struct ll_inode_info *lli = ll_i2info(inode);
1795         struct ll_sb_info *sbi = ll_i2sbi(inode);
1796
1797         ENTRY;
1798
1799         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
1800                PFID(ll_inode2fid(inode)), inode);
1801
1802         if (S_ISDIR(inode->i_mode)) {
1803                 /* these should have been cleared in ll_file_release */
1804                 LASSERT(lli->lli_opendir_key == NULL);
1805                 LASSERT(lli->lli_sai == NULL);
1806                 LASSERT(lli->lli_opendir_pid == 0);
1807         } else {
1808                 pcc_inode_free(inode);
1809         }
1810
1811         md_null_inode(sbi->ll_md_exp, ll_inode2fid(inode));
1812
1813         LASSERT(!lli->lli_open_fd_write_count);
1814         LASSERT(!lli->lli_open_fd_read_count);
1815         LASSERT(!lli->lli_open_fd_exec_count);
1816
1817         if (lli->lli_mds_write_och)
1818                 ll_md_real_close(inode, FMODE_WRITE);
1819         if (lli->lli_mds_exec_och)
1820                 ll_md_real_close(inode, FMODE_EXEC);
1821         if (lli->lli_mds_read_och)
1822                 ll_md_real_close(inode, FMODE_READ);
1823
1824         if (S_ISLNK(inode->i_mode) && lli->lli_symlink_name) {
1825                 OBD_FREE(lli->lli_symlink_name,
1826                          strlen(lli->lli_symlink_name) + 1);
1827                 lli->lli_symlink_name = NULL;
1828         }
1829
1830         ll_xattr_cache_destroy(inode);
1831
1832         forget_all_cached_acls(inode);
1833         lli_clear_acl(lli);
1834         lli->lli_inode_magic = LLI_INODE_DEAD;
1835
1836         if (S_ISDIR(inode->i_mode))
1837                 ll_dir_clear_lsm_md(inode);
1838         else if (S_ISREG(inode->i_mode) && !is_bad_inode(inode))
1839                 LASSERT(list_empty(&lli->lli_agl_list));
1840
1841         /*
1842          * XXX This has to be done before lsm is freed below, because
1843          * cl_object still uses inode lsm.
1844          */
1845         cl_inode_fini(inode);
1846
1847         llcrypt_put_encryption_info(inode);
1848
1849         EXIT;
1850 }
1851
1852 static int ll_md_setattr(struct dentry *dentry, struct md_op_data *op_data)
1853 {
1854         struct lustre_md md;
1855         struct inode *inode = dentry->d_inode;
1856         struct ll_sb_info *sbi = ll_i2sbi(inode);
1857         struct ptlrpc_request *request = NULL;
1858         int rc, ia_valid;
1859
1860         ENTRY;
1861
1862         op_data = ll_prep_md_op_data(op_data, inode, NULL, NULL, 0, 0,
1863                                      LUSTRE_OPC_ANY, NULL);
1864         if (IS_ERR(op_data))
1865                 RETURN(PTR_ERR(op_data));
1866
1867         /* If this is a chgrp of a regular file, we want to reserve enough
1868          * quota to cover the entire file size.
1869          */
1870         if (S_ISREG(inode->i_mode) && op_data->op_attr.ia_valid & ATTR_GID &&
1871             from_kgid(&init_user_ns, op_data->op_attr.ia_gid) !=
1872             from_kgid(&init_user_ns, inode->i_gid)) {
1873                 op_data->op_xvalid |= OP_XVALID_BLOCKS;
1874                 op_data->op_attr_blocks = inode->i_blocks;
1875         }
1876
1877
1878         rc = md_setattr(sbi->ll_md_exp, op_data, NULL, 0, &request);
1879         if (rc) {
1880                 ptlrpc_req_finished(request);
1881                 if (rc == -ENOENT) {
1882                         clear_nlink(inode);
1883                         /* Unlinked special device node? Or just a race?
1884                          * Pretend we done everything. */
1885                         if (!S_ISREG(inode->i_mode) &&
1886                             !S_ISDIR(inode->i_mode)) {
1887                                 ia_valid = op_data->op_attr.ia_valid;
1888                                 op_data->op_attr.ia_valid &= ~TIMES_SET_FLAGS;
1889                                 rc = simple_setattr(&init_user_ns, dentry,
1890                                                     &op_data->op_attr);
1891                                 op_data->op_attr.ia_valid = ia_valid;
1892                         }
1893                 } else if (rc != -EPERM && rc != -EACCES && rc != -ETXTBSY) {
1894                         CERROR("md_setattr fails: rc = %d\n", rc);
1895                 }
1896                 RETURN(rc);
1897         }
1898
1899         rc = md_get_lustre_md(sbi->ll_md_exp, &request->rq_pill, sbi->ll_dt_exp,
1900                               sbi->ll_md_exp, &md);
1901         if (rc) {
1902                 ptlrpc_req_finished(request);
1903                 RETURN(rc);
1904         }
1905
1906         ia_valid = op_data->op_attr.ia_valid;
1907         /* inode size will be in ll_setattr_ost, can't do it now since dirty
1908          * cache is not cleared yet. */
1909         op_data->op_attr.ia_valid &= ~(TIMES_SET_FLAGS | ATTR_SIZE);
1910         if (S_ISREG(inode->i_mode))
1911                 inode_lock(inode);
1912         rc = simple_setattr(&init_user_ns, dentry, &op_data->op_attr);
1913         if (S_ISREG(inode->i_mode))
1914                 inode_unlock(inode);
1915         op_data->op_attr.ia_valid = ia_valid;
1916
1917         rc = ll_update_inode(inode, &md);
1918         ptlrpc_req_finished(request);
1919
1920         RETURN(rc);
1921 }
1922
1923 /**
1924  * Zero portion of page that is part of @inode.
1925  * This implies, if necessary:
1926  * - taking cl_lock on range corresponding to concerned page
1927  * - grabbing vm page
1928  * - associating cl_page
1929  * - proceeding to clio read
1930  * - zeroing range in page
1931  * - proceeding to cl_page flush
1932  * - releasing cl_lock
1933  *
1934  * \param[in] inode     inode
1935  * \param[in] index     page index
1936  * \param[in] offset    offset in page to start zero from
1937  * \param[in] len       len to zero
1938  *
1939  * \retval 0            on success
1940  * \retval negative     errno on failure
1941  */
1942 int ll_io_zero_page(struct inode *inode, pgoff_t index, pgoff_t offset,
1943                     unsigned len)
1944 {
1945         struct ll_inode_info *lli = ll_i2info(inode);
1946         struct cl_object *clob = lli->lli_clob;
1947         __u16 refcheck;
1948         struct lu_env *env = NULL;
1949         struct cl_io *io = NULL;
1950         struct cl_page *clpage = NULL;
1951         struct page *vmpage = NULL;
1952         unsigned from = index << PAGE_SHIFT;
1953         struct cl_lock *lock = NULL;
1954         struct cl_lock_descr *descr = NULL;
1955         struct cl_2queue *queue = NULL;
1956         struct cl_sync_io *anchor = NULL;
1957         bool holdinglock = false;
1958         int rc;
1959
1960         ENTRY;
1961
1962         env = cl_env_get(&refcheck);
1963         if (IS_ERR(env))
1964                 RETURN(PTR_ERR(env));
1965
1966         io = vvp_env_thread_io(env);
1967         io->ci_obj = clob;
1968         rc = cl_io_rw_init(env, io, CIT_WRITE, from, PAGE_SIZE);
1969         if (rc)
1970                 GOTO(putenv, rc);
1971
1972         lock = vvp_env_lock(env);
1973         descr = &lock->cll_descr;
1974         descr->cld_obj   = io->ci_obj;
1975         descr->cld_start = cl_index(io->ci_obj, from);
1976         descr->cld_end   = cl_index(io->ci_obj, from + PAGE_SIZE - 1);
1977         descr->cld_mode  = CLM_WRITE;
1978         descr->cld_enq_flags = CEF_MUST | CEF_NONBLOCK;
1979
1980         /* request lock for page */
1981         rc = cl_lock_request(env, io, lock);
1982         /* -ECANCELED indicates a matching lock with a different extent
1983          * was already present, and -EEXIST indicates a matching lock
1984          * on exactly the same extent was already present.
1985          * In both cases it means we are covered.
1986          */
1987         if (rc == -ECANCELED || rc == -EEXIST)
1988                 rc = 0;
1989         else if (rc < 0)
1990                 GOTO(iofini, rc);
1991         else
1992                 holdinglock = true;
1993
1994         /* grab page */
1995         vmpage = grab_cache_page_nowait(inode->i_mapping, index);
1996         if (vmpage == NULL)
1997                 GOTO(rellock, rc = -EOPNOTSUPP);
1998
1999         if (!PageDirty(vmpage)) {
2000                 /* associate cl_page */
2001                 clpage = cl_page_find(env, clob, vmpage->index,
2002                                       vmpage, CPT_CACHEABLE);
2003                 if (IS_ERR(clpage))
2004                         GOTO(pagefini, rc = PTR_ERR(clpage));
2005
2006                 cl_page_assume(env, io, clpage);
2007         }
2008
2009         if (!PageUptodate(vmpage) && !PageDirty(vmpage) &&
2010             !PageWriteback(vmpage)) {
2011                 /* read page */
2012                 /* Set PagePrivate2 to detect special case of empty page
2013                  * in osc_brw_fini_request().
2014                  * It is also used to tell ll_io_read_page() that we do not
2015                  * want the vmpage to be unlocked.
2016                  */
2017                 SetPagePrivate2(vmpage);
2018                 rc = ll_io_read_page(env, io, clpage, NULL);
2019                 if (!PagePrivate2(vmpage)) {
2020                         /* PagePrivate2 was cleared in osc_brw_fini_request()
2021                          * meaning we read an empty page. In this case, in order
2022                          * to avoid allocating unnecessary block in truncated
2023                          * file, we must not zero and write as below. Subsequent
2024                          * server-side truncate will handle things correctly.
2025                          */
2026                         cl_page_unassume(env, io, clpage);
2027                         GOTO(clpfini, rc = 0);
2028                 }
2029                 ClearPagePrivate2(vmpage);
2030                 if (rc)
2031                         GOTO(clpfini, rc);
2032         }
2033
2034         /* Thanks to PagePrivate2 flag, ll_io_read_page() did not unlock
2035          * the vmpage, so we are good to proceed and zero range in page.
2036          */
2037         zero_user(vmpage, offset, len);
2038
2039         if (holdinglock && clpage) {
2040                 /* explicitly write newly modified page */
2041                 queue = &io->ci_queue;
2042                 cl_2queue_init(queue);
2043                 anchor = &vvp_env_info(env)->vti_anchor;
2044                 cl_sync_io_init(anchor, 1);
2045                 clpage->cp_sync_io = anchor;
2046                 cl_2queue_add(queue, clpage, true);
2047                 rc = cl_io_submit_rw(env, io, CRT_WRITE, queue);
2048                 if (rc)
2049                         GOTO(queuefini1, rc);
2050                 rc = cl_sync_io_wait(env, anchor, 0);
2051                 if (rc)
2052                         GOTO(queuefini2, rc);
2053                 cl_page_assume(env, io, clpage);
2054
2055 queuefini2:
2056                 cl_2queue_discard(env, io, queue);
2057 queuefini1:
2058                 cl_2queue_disown(env, queue);
2059                 cl_2queue_fini(env, queue);
2060         }
2061
2062 clpfini:
2063         if (clpage)
2064                 cl_page_put(env, clpage);
2065 pagefini:
2066         unlock_page(vmpage);
2067         put_page(vmpage);
2068 rellock:
2069         if (holdinglock)
2070                 cl_lock_release(env, lock);
2071 iofini:
2072         cl_io_fini(env, io);
2073 putenv:
2074         if (env)
2075                 cl_env_put(env, &refcheck);
2076
2077         RETURN(rc);
2078 }
2079
2080 /**
2081  * Get reference file from volatile file name.
2082  * Volatile file name may look like:
2083  * <parent>/LUSTRE_VOLATILE_HDR:<mdt_index>:<random>:fd=<fd>
2084  * where fd is opened descriptor of reference file.
2085  *
2086  * \param[in] volatile_name     volatile file name
2087  * \param[in] volatile_len      volatile file name length
2088  * \param[out] ref_file         pointer to struct file of reference file
2089  *
2090  * \retval 0            on success
2091  * \retval negative     errno on failure
2092  */
2093 int volatile_ref_file(const char *volatile_name, int volatile_len,
2094                       struct file **ref_file)
2095 {
2096         char *p, *q, *fd_str;
2097         int fd, rc;
2098
2099         p = strnstr(volatile_name, ":fd=", volatile_len);
2100         if (!p || strlen(p + 4) == 0)
2101                 return -EINVAL;
2102
2103         q = strchrnul(p + 4, ':');
2104         fd_str = kstrndup(p + 4, q - p - 4, GFP_NOFS);
2105         if (!fd_str)
2106                 return -ENOMEM;
2107         rc = kstrtouint(fd_str, 10, &fd);
2108         kfree(fd_str);
2109         if (rc)
2110                 return -EINVAL;
2111
2112         *ref_file = fget(fd);
2113         if (!(*ref_file))
2114                 return -EINVAL;
2115         return 0;
2116 }
2117
2118 /* If this inode has objects allocated to it (lsm != NULL), then the OST
2119  * object(s) determine the file size and mtime.  Otherwise, the MDS will
2120  * keep these values until such a time that objects are allocated for it.
2121  * We do the MDS operations first, as it is checking permissions for us.
2122  * We don't to the MDS RPC if there is nothing that we want to store there,
2123  * otherwise there is no harm in updating mtime/atime on the MDS if we are
2124  * going to do an RPC anyways.
2125  *
2126  * If we are doing a truncate, we will send the mtime and ctime updates
2127  * to the OST with the punch RPC, otherwise we do an explicit setattr RPC.
2128  * I don't believe it is possible to get e.g. ATTR_MTIME_SET and ATTR_SIZE
2129  * at the same time.
2130  *
2131  * In case of HSMimport, we only set attr on MDS.
2132  */
2133 int ll_setattr_raw(struct dentry *dentry, struct iattr *attr,
2134                    enum op_xvalid xvalid, bool hsm_import)
2135 {
2136         struct inode *inode = dentry->d_inode;
2137         struct ll_inode_info *lli = ll_i2info(inode);
2138         struct md_op_data *op_data = NULL;
2139         ktime_t kstart = ktime_get();
2140         int rc = 0;
2141
2142         ENTRY;
2143
2144         CDEBUG(D_VFSTRACE, "%s: setattr inode "DFID"(%p) from %llu to %llu, "
2145                "valid %x, hsm_import %d\n",
2146                ll_i2sbi(inode)->ll_fsname, PFID(&lli->lli_fid),
2147                inode, i_size_read(inode), attr->ia_size, attr->ia_valid,
2148                hsm_import);
2149
2150         if (attr->ia_valid & ATTR_SIZE) {
2151                 /* Check new size against VFS/VM file size limit and rlimit */
2152                 rc = inode_newsize_ok(inode, attr->ia_size);
2153                 if (rc)
2154                         RETURN(rc);
2155
2156                 /* The maximum Lustre file size is variable, based on the
2157                  * OST maximum object size and number of stripes.  This
2158                  * needs another check in addition to the VFS check above. */
2159                 if (attr->ia_size > ll_file_maxbytes(inode)) {
2160                         CDEBUG(D_INODE,"file "DFID" too large %llu > %llu\n",
2161                                PFID(&lli->lli_fid), attr->ia_size,
2162                                ll_file_maxbytes(inode));
2163                         RETURN(-EFBIG);
2164                 }
2165
2166                 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
2167         }
2168
2169         /* POSIX: check before ATTR_*TIME_SET set (from inode_change_ok) */
2170         if (attr->ia_valid & TIMES_SET_FLAGS) {
2171                 if ((!uid_eq(current_fsuid(), inode->i_uid)) &&
2172                     !capable(CAP_FOWNER))
2173                         RETURN(-EPERM);
2174         }
2175
2176         /* We mark all of the fields "set" so MDS/OST does not re-set them */
2177         if (!(xvalid & OP_XVALID_CTIME_SET) &&
2178              (attr->ia_valid & ATTR_CTIME)) {
2179                 attr->ia_ctime = current_time(inode);
2180                 xvalid |= OP_XVALID_CTIME_SET;
2181         }
2182         if (!(attr->ia_valid & ATTR_ATIME_SET) &&
2183             (attr->ia_valid & ATTR_ATIME)) {
2184                 attr->ia_atime = current_time(inode);
2185                 attr->ia_valid |= ATTR_ATIME_SET;
2186         }
2187         if (!(attr->ia_valid & ATTR_MTIME_SET) &&
2188             (attr->ia_valid & ATTR_MTIME)) {
2189                 attr->ia_mtime = current_time(inode);
2190                 attr->ia_valid |= ATTR_MTIME_SET;
2191         }
2192
2193         if (attr->ia_valid & (ATTR_MTIME | ATTR_CTIME))
2194                 CDEBUG(D_INODE, "setting mtime %lld, ctime %lld, now = %lld\n",
2195                        (s64)attr->ia_mtime.tv_sec, (s64)attr->ia_ctime.tv_sec,
2196                        ktime_get_real_seconds());
2197
2198         if (S_ISREG(inode->i_mode))
2199                 inode_unlock(inode);
2200
2201         /* We always do an MDS RPC, even if we're only changing the size;
2202          * only the MDS knows whether truncate() should fail with -ETXTBUSY */
2203
2204         OBD_ALLOC_PTR(op_data);
2205         if (op_data == NULL)
2206                 GOTO(out, rc = -ENOMEM);
2207
2208         if (!hsm_import && attr->ia_valid & ATTR_SIZE) {
2209                 /* If we are changing file size, file content is
2210                  * modified, flag it.
2211                  */
2212                 xvalid |= OP_XVALID_OWNEROVERRIDE;
2213                 op_data->op_bias |= MDS_DATA_MODIFIED;
2214                 clear_bit(LLIF_DATA_MODIFIED, &lli->lli_flags);
2215         }
2216
2217         if (attr->ia_valid & ATTR_FILE) {
2218                 struct ll_file_data *fd = attr->ia_file->private_data;
2219
2220                 if (fd->fd_lease_och)
2221                         op_data->op_bias |= MDS_TRUNC_KEEP_LEASE;
2222         }
2223
2224         op_data->op_attr = *attr;
2225         op_data->op_xvalid = xvalid;
2226
2227         rc = ll_md_setattr(dentry, op_data);
2228         if (rc)
2229                 GOTO(out, rc);
2230
2231         if (!S_ISREG(inode->i_mode) || hsm_import)
2232                 GOTO(out, rc = 0);
2233
2234         if (attr->ia_valid & (ATTR_SIZE | ATTR_ATIME | ATTR_ATIME_SET |
2235                               ATTR_MTIME | ATTR_MTIME_SET | ATTR_CTIME) ||
2236             xvalid & OP_XVALID_CTIME_SET) {
2237                 bool cached = false;
2238
2239                 rc = pcc_inode_setattr(inode, attr, &cached);
2240                 if (cached) {
2241                         if (rc) {
2242                                 CERROR("%s: PCC inode "DFID" setattr failed: "
2243                                        "rc = %d\n",
2244                                        ll_i2sbi(inode)->ll_fsname,
2245                                        PFID(&lli->lli_fid), rc);
2246                                 GOTO(out, rc);
2247                         }
2248                 } else {
2249                         unsigned int flags = 0;
2250
2251                         /* For truncate and utimes sending attributes to OSTs,
2252                          * setting mtime/atime to the past will be performed
2253                          * under PW [0:EOF] extent lock (new_size:EOF for
2254                          * truncate). It may seem excessive to send mtime/atime
2255                          * updates to OSTs when not setting times to past, but
2256                          * it is necessary due to possible time
2257                          * de-synchronization between MDT inode and OST objects
2258                          */
2259                         if (S_ISREG(inode->i_mode) && IS_ENCRYPTED(inode)) {
2260                                 xvalid |= OP_XVALID_FLAGS;
2261                                 flags = LUSTRE_ENCRYPT_FL;
2262                                 /* Call to ll_io_zero_page is not necessary if
2263                                  * truncating on PAGE_SIZE boundary, because
2264                                  * whole pages will be wiped.
2265                                  * In case of Direct IO, all we need is to set
2266                                  * new size.
2267                                  */
2268                                 if (attr->ia_valid & ATTR_SIZE &&
2269                                     attr->ia_size & ~PAGE_MASK &&
2270                                     !(attr->ia_valid & ATTR_FILE &&
2271                                       attr->ia_file->f_flags & O_DIRECT)) {
2272                                         pgoff_t offset =
2273                                                 attr->ia_size & (PAGE_SIZE - 1);
2274
2275                                         rc = ll_io_zero_page(inode,
2276                                                     attr->ia_size >> PAGE_SHIFT,
2277                                                     offset, PAGE_SIZE - offset);
2278                                         if (rc)
2279                                                 GOTO(out, rc);
2280                                 }
2281                                 /* If encrypted volatile file without the key,
2282                                  * we need to fetch size from reference file,
2283                                  * and set it on OST objects. This happens when
2284                                  * migrating or extending an encrypted file
2285                                  * without the key.
2286                                  */
2287                                 if (filename_is_volatile(dentry->d_name.name,
2288                                                          dentry->d_name.len,
2289                                                          NULL) &&
2290                                     llcrypt_require_key(inode) == -ENOKEY) {
2291                                         struct file *ref_file;
2292                                         struct inode *ref_inode;
2293                                         struct ll_inode_info *ref_lli;
2294                                         struct cl_object *ref_obj;
2295                                         struct cl_attr ref_attr = { 0 };
2296                                         struct lu_env *env;
2297                                         __u16 refcheck;
2298
2299                                         rc = volatile_ref_file(
2300                                                 dentry->d_name.name,
2301                                                 dentry->d_name.len,
2302                                                 &ref_file);
2303                                         if (rc)
2304                                                 GOTO(out, rc);
2305
2306                                         ref_inode = file_inode(ref_file);
2307                                         if (!ref_inode) {
2308                                                 fput(ref_file);
2309                                                 GOTO(out, rc = -EINVAL);
2310                                         }
2311
2312                                         env = cl_env_get(&refcheck);
2313                                         if (IS_ERR(env))
2314                                                 GOTO(out, rc = PTR_ERR(env));
2315
2316                                         ref_lli = ll_i2info(ref_inode);
2317                                         ref_obj = ref_lli->lli_clob;
2318                                         cl_object_attr_lock(ref_obj);
2319                                         rc = cl_object_attr_get(env, ref_obj,
2320                                                                 &ref_attr);
2321                                         cl_object_attr_unlock(ref_obj);
2322                                         cl_env_put(env, &refcheck);
2323                                         fput(ref_file);
2324                                         if (rc)
2325                                                 GOTO(out, rc);
2326
2327                                         attr->ia_valid |= ATTR_SIZE;
2328                                         attr->ia_size = ref_attr.cat_size;
2329                                 }
2330                         }
2331                         rc = cl_setattr_ost(lli->lli_clob, attr, xvalid, flags);
2332                 }
2333         }
2334
2335         /* If the file was restored, it needs to set dirty flag.
2336          *
2337          * We've already sent MDS_DATA_MODIFIED flag in
2338          * ll_md_setattr() for truncate. However, the MDT refuses to
2339          * set the HS_DIRTY flag on released files, so we have to set
2340          * it again if the file has been restored. Please check how
2341          * LLIF_DATA_MODIFIED is set in vvp_io_setattr_fini().
2342          *
2343          * Please notice that if the file is not released, the previous
2344          * MDS_DATA_MODIFIED has taken effect and usually
2345          * LLIF_DATA_MODIFIED is not set(see vvp_io_setattr_fini()).
2346          * This way we can save an RPC for common open + trunc
2347          * operation. */
2348         if (test_and_clear_bit(LLIF_DATA_MODIFIED, &lli->lli_flags)) {
2349                 struct hsm_state_set hss = {
2350                         .hss_valid = HSS_SETMASK,
2351                         .hss_setmask = HS_DIRTY,
2352                 };
2353                 int rc2;
2354
2355                 rc2 = ll_hsm_state_set(inode, &hss);
2356                 /* truncate and write can happen at the same time, so that
2357                  * the file can be set modified even though the file is not
2358                  * restored from released state, and ll_hsm_state_set() is
2359                  * not applicable for the file, and rc2 < 0 is normal in this
2360                  * case. */
2361                 if (rc2 < 0)
2362                         CDEBUG(D_INFO, DFID "HSM set dirty failed: rc2 = %d\n",
2363                                PFID(ll_inode2fid(inode)), rc2);
2364         }
2365
2366         EXIT;
2367 out:
2368         if (op_data != NULL)
2369                 ll_finish_md_op_data(op_data);
2370
2371         if (S_ISREG(inode->i_mode)) {
2372                 inode_lock(inode);
2373                 if ((attr->ia_valid & ATTR_SIZE) && !hsm_import)
2374                         inode_dio_wait(inode);
2375                 /* Once we've got the i_mutex, it's safe to set the S_NOSEC
2376                  * flag.  ll_update_inode (called from ll_md_setattr), clears
2377                  * inode flags, so there is a gap where S_NOSEC is not set.
2378                  * This can cause a writer to take the i_mutex unnecessarily,
2379                  * but this is safe to do and should be rare. */
2380                 inode_has_no_xattr(inode);
2381         }
2382
2383         if (!rc)
2384                 ll_stats_ops_tally(ll_i2sbi(inode), attr->ia_valid & ATTR_SIZE ?
2385                                         LPROC_LL_TRUNC : LPROC_LL_SETATTR,
2386                                    ktime_us_delta(ktime_get(), kstart));
2387
2388         RETURN(rc);
2389 }
2390
2391 int ll_setattr(struct user_namespace *mnt_userns, struct dentry *de,
2392                struct iattr *attr)
2393 {
2394         int mode = de->d_inode->i_mode;
2395         enum op_xvalid xvalid = 0;
2396         int rc;
2397
2398         rc = llcrypt_prepare_setattr(de, attr);
2399         if (rc)
2400                 return rc;
2401
2402         if ((attr->ia_valid & (ATTR_CTIME|ATTR_SIZE|ATTR_MODE)) ==
2403                               (ATTR_CTIME|ATTR_SIZE|ATTR_MODE))
2404                 xvalid |= OP_XVALID_OWNEROVERRIDE;
2405
2406         if (((attr->ia_valid & (ATTR_MODE|ATTR_FORCE|ATTR_SIZE)) ==
2407                                (ATTR_SIZE|ATTR_MODE)) &&
2408             (((mode & S_ISUID) && !(attr->ia_mode & S_ISUID)) ||
2409              (((mode & (S_ISGID|S_IXGRP)) == (S_ISGID|S_IXGRP)) &&
2410               !(attr->ia_mode & S_ISGID))))
2411                 attr->ia_valid |= ATTR_FORCE;
2412
2413         if ((attr->ia_valid & ATTR_MODE) &&
2414             (mode & S_ISUID) &&
2415             !(attr->ia_mode & S_ISUID) &&
2416             !(attr->ia_valid & ATTR_KILL_SUID))
2417                 attr->ia_valid |= ATTR_KILL_SUID;
2418
2419         if ((attr->ia_valid & ATTR_MODE) &&
2420             ((mode & (S_ISGID|S_IXGRP)) == (S_ISGID|S_IXGRP)) &&
2421             !(attr->ia_mode & S_ISGID) &&
2422             !(attr->ia_valid & ATTR_KILL_SGID))
2423                 attr->ia_valid |= ATTR_KILL_SGID;
2424
2425         return ll_setattr_raw(de, attr, xvalid, false);
2426 }
2427
2428 int ll_statfs_internal(struct ll_sb_info *sbi, struct obd_statfs *osfs,
2429                        u32 flags)
2430 {
2431         struct obd_statfs obd_osfs = { 0 };
2432         time64_t max_age;
2433         int rc;
2434
2435         ENTRY;
2436         max_age = ktime_get_seconds() - sbi->ll_statfs_max_age;
2437
2438         if (test_bit(LL_SBI_LAZYSTATFS, sbi->ll_flags))
2439                 flags |= OBD_STATFS_NODELAY;
2440
2441         rc = obd_statfs(NULL, sbi->ll_md_exp, osfs, max_age, flags);
2442         if (rc)
2443                 RETURN(rc);
2444
2445         osfs->os_type = LL_SUPER_MAGIC;
2446
2447         CDEBUG(D_SUPER, "MDC blocks %llu/%llu objects %llu/%llu\n",
2448               osfs->os_bavail, osfs->os_blocks, osfs->os_ffree, osfs->os_files);
2449
2450         if (osfs->os_state & OS_STATFS_SUM)
2451                 GOTO(out, rc);
2452
2453         rc = obd_statfs(NULL, sbi->ll_dt_exp, &obd_osfs, max_age, flags);
2454         if (rc) /* Possibly a filesystem with no OSTs.  Report MDT totals. */
2455                 GOTO(out, rc = 0);
2456
2457         CDEBUG(D_SUPER, "OSC blocks %llu/%llu objects %llu/%llu\n",
2458                obd_osfs.os_bavail, obd_osfs.os_blocks, obd_osfs.os_ffree,
2459                obd_osfs.os_files);
2460
2461         osfs->os_bsize = obd_osfs.os_bsize;
2462         osfs->os_blocks = obd_osfs.os_blocks;
2463         osfs->os_bfree = obd_osfs.os_bfree;
2464         osfs->os_bavail = obd_osfs.os_bavail;
2465
2466         /* If we have _some_ OSTs, but don't have as many free objects on the
2467          * OSTs as inodes on the MDTs, reduce the reported number of inodes
2468          * to compensate, so that the "inodes in use" number is correct.
2469          * This should be kept in sync with lod_statfs() behaviour.
2470          */
2471         if (obd_osfs.os_files && obd_osfs.os_ffree < osfs->os_ffree) {
2472                 osfs->os_files = (osfs->os_files - osfs->os_ffree) +
2473                                  obd_osfs.os_ffree;
2474                 osfs->os_ffree = obd_osfs.os_ffree;
2475         }
2476
2477 out:
2478         RETURN(rc);
2479 }
2480
2481 static int ll_statfs_project(struct inode *inode, struct kstatfs *sfs)
2482 {
2483         struct if_quotactl qctl = {
2484                 .qc_cmd = LUSTRE_Q_GETQUOTA,
2485                 .qc_type = PRJQUOTA,
2486                 .qc_valid = QC_GENERAL,
2487         };
2488         u64 limit, curblock;
2489         int ret;
2490
2491         qctl.qc_id = ll_i2info(inode)->lli_projid;
2492         ret = quotactl_ioctl(inode->i_sb, &qctl);
2493         if (ret) {
2494                 /* ignore errors if project ID does not have
2495                  * a quota limit or feature unsupported.
2496                  */
2497                 if (ret == -ESRCH || ret == -EOPNOTSUPP)
2498                         ret = 0;
2499                 return ret;
2500         }
2501
2502         limit = ((qctl.qc_dqblk.dqb_bsoftlimit ?
2503                  qctl.qc_dqblk.dqb_bsoftlimit :
2504                  qctl.qc_dqblk.dqb_bhardlimit) * 1024) / sfs->f_bsize;
2505         if (limit && sfs->f_blocks > limit) {
2506                 curblock = (qctl.qc_dqblk.dqb_curspace +
2507                                 sfs->f_bsize - 1) / sfs->f_bsize;
2508                 sfs->f_blocks = limit;
2509                 sfs->f_bfree = sfs->f_bavail =
2510                         (sfs->f_blocks > curblock) ?
2511                         (sfs->f_blocks - curblock) : 0;
2512         }
2513
2514         limit = qctl.qc_dqblk.dqb_isoftlimit ?
2515                 qctl.qc_dqblk.dqb_isoftlimit :
2516                 qctl.qc_dqblk.dqb_ihardlimit;
2517         if (limit && sfs->f_files > limit) {
2518                 sfs->f_files = limit;
2519                 sfs->f_ffree = (sfs->f_files >
2520                         qctl.qc_dqblk.dqb_curinodes) ?
2521                         (sfs->f_files - qctl.qc_dqblk.dqb_curinodes) : 0;
2522         }
2523
2524         return 0;
2525 }
2526
2527 int ll_statfs(struct dentry *de, struct kstatfs *sfs)
2528 {
2529         struct super_block *sb = de->d_sb;
2530         struct obd_statfs osfs;
2531         __u64 fsid = huge_encode_dev(sb->s_dev);
2532         ktime_t kstart = ktime_get();
2533         int rc;
2534
2535         CDEBUG(D_VFSTRACE, "VFS Op:sb=%s (%p)\n", sb->s_id, sb);
2536
2537         /* Some amount of caching on the client is allowed */
2538         rc = ll_statfs_internal(ll_s2sbi(sb), &osfs, OBD_STATFS_SUM);
2539         if (rc)
2540                 return rc;
2541
2542         statfs_unpack(sfs, &osfs);
2543
2544         /* We need to downshift for all 32-bit kernels, because we can't
2545          * tell if the kernel is being called via sys_statfs64() or not.
2546          * Stop before overflowing f_bsize - in which case it is better
2547          * to just risk EOVERFLOW if caller is using old sys_statfs(). */
2548         if (sizeof(long) < 8) {
2549                 while (osfs.os_blocks > ~0UL && sfs->f_bsize < 0x40000000) {
2550                         sfs->f_bsize <<= 1;
2551
2552                         osfs.os_blocks >>= 1;
2553                         osfs.os_bfree >>= 1;
2554                         osfs.os_bavail >>= 1;
2555                 }
2556         }
2557
2558         sfs->f_blocks = osfs.os_blocks;
2559         sfs->f_bfree = osfs.os_bfree;
2560         sfs->f_bavail = osfs.os_bavail;
2561         sfs->f_fsid.val[0] = (__u32)fsid;
2562         sfs->f_fsid.val[1] = (__u32)(fsid >> 32);
2563         if (ll_i2info(de->d_inode)->lli_projid)
2564                 return ll_statfs_project(de->d_inode, sfs);
2565
2566         ll_stats_ops_tally(ll_s2sbi(sb), LPROC_LL_STATFS,
2567                            ktime_us_delta(ktime_get(), kstart));
2568
2569         return 0;
2570 }
2571
2572 void ll_inode_size_lock(struct inode *inode)
2573 {
2574         struct ll_inode_info *lli;
2575
2576         LASSERT(!S_ISDIR(inode->i_mode));
2577
2578         lli = ll_i2info(inode);
2579         mutex_lock(&lli->lli_size_mutex);
2580 }
2581
2582 void ll_inode_size_unlock(struct inode *inode)
2583 {
2584         struct ll_inode_info *lli;
2585
2586         lli = ll_i2info(inode);
2587         mutex_unlock(&lli->lli_size_mutex);
2588 }
2589
2590 void ll_update_inode_flags(struct inode *inode, unsigned int ext_flags)
2591 {
2592         /* do not clear encryption flag */
2593         ext_flags |= ll_inode_to_ext_flags(inode->i_flags) & LUSTRE_ENCRYPT_FL;
2594         inode->i_flags = ll_ext_to_inode_flags(ext_flags);
2595         if (ext_flags & LUSTRE_PROJINHERIT_FL)
2596                 set_bit(LLIF_PROJECT_INHERIT, &ll_i2info(inode)->lli_flags);
2597         else
2598                 clear_bit(LLIF_PROJECT_INHERIT, &ll_i2info(inode)->lli_flags);
2599 }
2600
2601 int ll_update_inode(struct inode *inode, struct lustre_md *md)
2602 {
2603         struct ll_inode_info *lli = ll_i2info(inode);
2604         struct mdt_body *body = md->body;
2605         struct ll_sb_info *sbi = ll_i2sbi(inode);
2606         bool api32;
2607         int rc = 0;
2608
2609         if (body->mbo_valid & OBD_MD_FLEASIZE) {
2610                 rc = cl_file_inode_init(inode, md);
2611                 if (rc)
2612                         return rc;
2613         }
2614
2615         if (S_ISDIR(inode->i_mode)) {
2616                 rc = ll_update_lsm_md(inode, md);
2617                 if (rc != 0)
2618                         return rc;
2619         }
2620
2621         if (body->mbo_valid & OBD_MD_FLACL)
2622                 lli_replace_acl(lli, md);
2623
2624         api32 = test_bit(LL_SBI_32BIT_API, sbi->ll_flags);
2625         inode->i_ino = cl_fid_build_ino(&body->mbo_fid1, api32);
2626         inode->i_generation = cl_fid_build_gen(&body->mbo_fid1);
2627
2628         if (body->mbo_valid & OBD_MD_FLATIME) {
2629                 if (body->mbo_atime > inode->i_atime.tv_sec)
2630                         inode->i_atime.tv_sec = body->mbo_atime;
2631                 lli->lli_atime = body->mbo_atime;
2632         }
2633
2634         if (body->mbo_valid & OBD_MD_FLMTIME) {
2635                 if (body->mbo_mtime > inode->i_mtime.tv_sec) {
2636                         CDEBUG(D_INODE,
2637                                "setting ino %lu mtime from %lld to %llu\n",
2638                                inode->i_ino, (s64)inode->i_mtime.tv_sec,
2639                                body->mbo_mtime);
2640                         inode->i_mtime.tv_sec = body->mbo_mtime;
2641                 }
2642                 lli->lli_mtime = body->mbo_mtime;
2643         }
2644
2645         if (body->mbo_valid & OBD_MD_FLCTIME) {
2646                 if (body->mbo_ctime > inode->i_ctime.tv_sec)
2647                         inode->i_ctime.tv_sec = body->mbo_ctime;
2648                 lli->lli_ctime = body->mbo_ctime;
2649         }
2650
2651         if (body->mbo_valid & OBD_MD_FLBTIME)
2652                 lli->lli_btime = body->mbo_btime;
2653
2654         /* Clear i_flags to remove S_NOSEC before permissions are updated */
2655         if (body->mbo_valid & OBD_MD_FLFLAGS)
2656                 ll_update_inode_flags(inode, body->mbo_flags);
2657         if (body->mbo_valid & OBD_MD_FLMODE)
2658                 inode->i_mode = (inode->i_mode & S_IFMT) |
2659                                 (body->mbo_mode & ~S_IFMT);
2660
2661         if (body->mbo_valid & OBD_MD_FLTYPE)
2662                 inode->i_mode = (inode->i_mode & ~S_IFMT) |
2663                                 (body->mbo_mode & S_IFMT);
2664
2665         LASSERT(inode->i_mode != 0);
2666         if (body->mbo_valid & OBD_MD_FLUID)
2667                 inode->i_uid = make_kuid(&init_user_ns, body->mbo_uid);
2668         if (body->mbo_valid & OBD_MD_FLGID)
2669                 inode->i_gid = make_kgid(&init_user_ns, body->mbo_gid);
2670         if (body->mbo_valid & OBD_MD_FLPROJID)
2671                 lli->lli_projid = body->mbo_projid;
2672         if (body->mbo_valid & OBD_MD_FLNLINK) {
2673                 spin_lock(&inode->i_lock);
2674                 set_nlink(inode, body->mbo_nlink);
2675                 spin_unlock(&inode->i_lock);
2676         }
2677         if (body->mbo_valid & OBD_MD_FLRDEV)
2678                 inode->i_rdev = old_decode_dev(body->mbo_rdev);
2679
2680         if (body->mbo_valid & OBD_MD_FLID) {
2681                 /* FID shouldn't be changed! */
2682                 if (fid_is_sane(&lli->lli_fid)) {
2683                         LASSERTF(lu_fid_eq(&lli->lli_fid, &body->mbo_fid1),
2684                                  "Trying to change FID "DFID
2685                                  " to the "DFID", inode "DFID"(%p)\n",
2686                                  PFID(&lli->lli_fid), PFID(&body->mbo_fid1),
2687                                  PFID(ll_inode2fid(inode)), inode);
2688                 } else {
2689                         lli->lli_fid = body->mbo_fid1;
2690                 }
2691         }
2692
2693         LASSERT(fid_seq(&lli->lli_fid) != 0);
2694
2695         /* In case of encrypted file without the key, please do not lose
2696          * clear text size stored into lli_lazysize in ll_merge_attr(),
2697          * we will need it in ll_prepare_close().
2698          */
2699         if (lli->lli_attr_valid & OBD_MD_FLLAZYSIZE && lli->lli_lazysize &&
2700             llcrypt_require_key(inode) == -ENOKEY)
2701                 lli->lli_attr_valid = body->mbo_valid | OBD_MD_FLLAZYSIZE;
2702         else
2703                 lli->lli_attr_valid = body->mbo_valid;
2704         if (body->mbo_valid & OBD_MD_FLSIZE) {
2705                 i_size_write(inode, body->mbo_size);
2706
2707                 CDEBUG(D_VFSTRACE, "inode="DFID", updating i_size %llu\n",
2708                        PFID(ll_inode2fid(inode)),
2709                        (unsigned long long)body->mbo_size);
2710
2711                 if (body->mbo_valid & OBD_MD_FLBLOCKS)
2712                         inode->i_blocks = body->mbo_blocks;
2713         } else {
2714                 if (body->mbo_valid & OBD_MD_FLLAZYSIZE)
2715                         lli->lli_lazysize = body->mbo_size;
2716                 if (body->mbo_valid & OBD_MD_FLLAZYBLOCKS)
2717                         lli->lli_lazyblocks = body->mbo_blocks;
2718         }
2719
2720         if (body->mbo_valid & OBD_MD_TSTATE) {
2721                 /* Set LLIF_FILE_RESTORING if restore ongoing and
2722                  * clear it when done to ensure to start again
2723                  * glimpsing updated attrs
2724                  */
2725                 if (body->mbo_t_state & MS_RESTORE)
2726                         set_bit(LLIF_FILE_RESTORING, &lli->lli_flags);
2727                 else
2728                         clear_bit(LLIF_FILE_RESTORING, &lli->lli_flags);
2729         }
2730
2731         return 0;
2732 }
2733
2734 /* update directory depth to ROOT, called after LOOKUP lock is fetched. */
2735 void ll_update_dir_depth(struct inode *dir, struct inode *inode)
2736 {
2737         struct ll_inode_info *lli;
2738
2739         if (!S_ISDIR(inode->i_mode))
2740                 return;
2741
2742         if (inode == dir)
2743                 return;
2744
2745         lli = ll_i2info(inode);
2746         lli->lli_dir_depth = ll_i2info(dir)->lli_dir_depth + 1;
2747         CDEBUG(D_INODE, DFID" depth %hu\n",
2748                PFID(&lli->lli_fid), lli->lli_dir_depth);
2749 }
2750
2751 void ll_truncate_inode_pages_final(struct inode *inode)
2752 {
2753         struct address_space *mapping = &inode->i_data;
2754         unsigned long nrpages;
2755         unsigned long flags;
2756
2757         truncate_inode_pages_final(mapping);
2758
2759         /* Workaround for LU-118: Note nrpages may not be totally updated when
2760          * truncate_inode_pages() returns, as there can be a page in the process
2761          * of deletion (inside __delete_from_page_cache()) in the specified
2762          * range. Thus mapping->nrpages can be non-zero when this function
2763          * returns even after truncation of the whole mapping.  Only do this if
2764          * npages isn't already zero.
2765          */
2766         nrpages = mapping->nrpages;
2767         if (nrpages) {
2768                 ll_xa_lock_irqsave(&mapping->i_pages, flags);
2769                 nrpages = mapping->nrpages;
2770                 ll_xa_unlock_irqrestore(&mapping->i_pages, flags);
2771         } /* Workaround end */
2772
2773         LASSERTF(nrpages == 0, "%s: inode="DFID"(%p) nrpages=%lu, "
2774                  "see https://jira.whamcloud.com/browse/LU-118\n",
2775                  ll_i2sbi(inode)->ll_fsname,
2776                  PFID(ll_inode2fid(inode)), inode, nrpages);
2777 }
2778
2779 int ll_read_inode2(struct inode *inode, void *opaque)
2780 {
2781         struct lustre_md *md = opaque;
2782         struct ll_inode_info *lli = ll_i2info(inode);
2783         int     rc;
2784         ENTRY;
2785
2786         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
2787                PFID(&lli->lli_fid), inode);
2788
2789         /* Core attributes from the MDS first.  This is a new inode, and
2790          * the VFS doesn't zero times in the core inode so we have to do
2791          * it ourselves.  They will be overwritten by either MDS or OST
2792          * attributes - we just need to make sure they aren't newer.
2793          */
2794         inode->i_mtime.tv_sec = 0;
2795         inode->i_atime.tv_sec = 0;
2796         inode->i_ctime.tv_sec = 0;
2797         inode->i_rdev = 0;
2798         rc = ll_update_inode(inode, md);
2799         if (rc != 0)
2800                 RETURN(rc);
2801
2802         /* OIDEBUG(inode); */
2803
2804 #ifdef HAVE_BACKING_DEV_INFO
2805         /* initializing backing dev info. */
2806         inode->i_mapping->backing_dev_info = &s2lsi(inode->i_sb)->lsi_bdi;
2807 #endif
2808         if (S_ISREG(inode->i_mode)) {
2809                 struct ll_sb_info *sbi = ll_i2sbi(inode);
2810                 inode->i_op = &ll_file_inode_operations;
2811                 inode->i_fop = sbi->ll_fop;
2812                 inode->i_mapping->a_ops = (struct address_space_operations *)&ll_aops;
2813                 EXIT;
2814         } else if (S_ISDIR(inode->i_mode)) {
2815                 inode->i_op = &ll_dir_inode_operations;
2816                 inode->i_fop = &ll_dir_operations;
2817                 EXIT;
2818         } else if (S_ISLNK(inode->i_mode)) {
2819                 inode->i_op = &ll_fast_symlink_inode_operations;
2820                 EXIT;
2821         } else {
2822                 inode->i_op = &ll_special_inode_operations;
2823
2824                 init_special_inode(inode, inode->i_mode,
2825                                    inode->i_rdev);
2826
2827                 EXIT;
2828         }
2829
2830         return 0;
2831 }
2832
2833 void ll_delete_inode(struct inode *inode)
2834 {
2835         struct ll_inode_info *lli = ll_i2info(inode);
2836         ENTRY;
2837
2838         if (S_ISREG(inode->i_mode) && lli->lli_clob != NULL) {
2839                 /* It is last chance to write out dirty pages,
2840                  * otherwise we may lose data while umount.
2841                  *
2842                  * If i_nlink is 0 then just discard data. This is safe because
2843                  * local inode gets i_nlink 0 from server only for the last
2844                  * unlink, so that file is not opened somewhere else
2845                  */
2846                 cl_sync_file_range(inode, 0, OBD_OBJECT_EOF, inode->i_nlink ?
2847                                    CL_FSYNC_LOCAL : CL_FSYNC_DISCARD, 1);
2848         }
2849
2850         ll_truncate_inode_pages_final(inode);
2851         ll_clear_inode(inode);
2852         clear_inode(inode);
2853
2854         EXIT;
2855 }
2856
2857 int ll_iocontrol(struct inode *inode, struct file *file,
2858                  unsigned int cmd, unsigned long arg)
2859 {
2860         struct ll_sb_info *sbi = ll_i2sbi(inode);
2861         struct ptlrpc_request *req = NULL;
2862         int rc, flags = 0;
2863         ENTRY;
2864
2865         switch (cmd) {
2866         case FS_IOC_GETFLAGS: {
2867                 struct mdt_body *body;
2868                 struct md_op_data *op_data;
2869
2870                 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL,
2871                                              0, 0, LUSTRE_OPC_ANY,
2872                                              NULL);
2873                 if (IS_ERR(op_data))
2874                         RETURN(PTR_ERR(op_data));
2875
2876                 op_data->op_valid = OBD_MD_FLFLAGS;
2877                 rc = md_getattr(sbi->ll_md_exp, op_data, &req);
2878                 ll_finish_md_op_data(op_data);
2879                 if (rc) {
2880                         CERROR("%s: failure inode "DFID": rc = %d\n",
2881                                sbi->ll_md_exp->exp_obd->obd_name,
2882                                PFID(ll_inode2fid(inode)), rc);
2883                         RETURN(-abs(rc));
2884                 }
2885
2886                 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
2887
2888                 flags = body->mbo_flags;
2889
2890                 ptlrpc_req_finished(req);
2891
2892                 RETURN(put_user(flags, (int __user *)arg));
2893         }
2894         case FS_IOC_SETFLAGS: {
2895                 struct iattr *attr;
2896                 struct md_op_data *op_data;
2897                 struct cl_object *obj;
2898                 struct fsxattr fa = { 0 };
2899
2900                 if (get_user(flags, (int __user *)arg))
2901                         RETURN(-EFAULT);
2902
2903                 fa.fsx_projid = ll_i2info(inode)->lli_projid;
2904                 if (flags & LUSTRE_PROJINHERIT_FL)
2905                         fa.fsx_xflags = FS_XFLAG_PROJINHERIT;
2906
2907                 rc = ll_ioctl_check_project(inode, fa.fsx_xflags,
2908                                             fa.fsx_projid);
2909                 if (rc)
2910                         RETURN(rc);
2911
2912                 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
2913                                              LUSTRE_OPC_ANY, NULL);
2914                 if (IS_ERR(op_data))
2915                         RETURN(PTR_ERR(op_data));
2916
2917                 op_data->op_attr_flags = flags;
2918                 op_data->op_xvalid |= OP_XVALID_FLAGS;
2919                 rc = md_setattr(sbi->ll_md_exp, op_data, NULL, 0, &req);
2920                 ll_finish_md_op_data(op_data);
2921                 ptlrpc_req_finished(req);
2922                 if (rc)
2923                         RETURN(rc);
2924
2925                 ll_update_inode_flags(inode, flags);
2926
2927                 obj = ll_i2info(inode)->lli_clob;
2928                 if (obj == NULL)
2929                         RETURN(0);
2930
2931                 OBD_ALLOC_PTR(attr);
2932                 if (attr == NULL)
2933                         RETURN(-ENOMEM);
2934
2935                 rc = cl_setattr_ost(obj, attr, OP_XVALID_FLAGS, flags);
2936
2937                 OBD_FREE_PTR(attr);
2938                 RETURN(rc);
2939         }
2940         default:
2941                 RETURN(-ENOSYS);
2942         }
2943
2944         RETURN(0);
2945 }
2946
2947 int ll_flush_ctx(struct inode *inode)
2948 {
2949         struct ll_sb_info  *sbi = ll_i2sbi(inode);
2950
2951         CDEBUG(D_SEC, "flush context for user %d\n",
2952                from_kuid(&init_user_ns, current_uid()));
2953
2954         obd_set_info_async(NULL, sbi->ll_md_exp,
2955                            sizeof(KEY_FLUSH_CTX), KEY_FLUSH_CTX,
2956                            0, NULL, NULL);
2957         obd_set_info_async(NULL, sbi->ll_dt_exp,
2958                            sizeof(KEY_FLUSH_CTX), KEY_FLUSH_CTX,
2959                            0, NULL, NULL);
2960         return 0;
2961 }
2962
2963 /* umount -f client means force down, don't save state */
2964 void ll_umount_begin(struct super_block *sb)
2965 {
2966         struct ll_sb_info *sbi = ll_s2sbi(sb);
2967         struct obd_device *obd;
2968         struct obd_ioctl_data *ioc_data;
2969         int cnt;
2970         ENTRY;
2971
2972         CDEBUG(D_VFSTRACE, "VFS Op: superblock %p count %d active %d\n", sb,
2973                sb->s_count, atomic_read(&sb->s_active));
2974
2975         obd = class_exp2obd(sbi->ll_md_exp);
2976         if (obd == NULL) {
2977                 CERROR("Invalid MDC connection handle %#llx\n",
2978                        sbi->ll_md_exp->exp_handle.h_cookie);
2979                 EXIT;
2980                 return;
2981         }
2982         obd->obd_force = 1;
2983
2984         obd = class_exp2obd(sbi->ll_dt_exp);
2985         if (obd == NULL) {
2986                 CERROR("Invalid LOV connection handle %#llx\n",
2987                        sbi->ll_dt_exp->exp_handle.h_cookie);
2988                 EXIT;
2989                 return;
2990         }
2991         obd->obd_force = 1;
2992
2993         OBD_ALLOC_PTR(ioc_data);
2994         if (ioc_data) {
2995                 obd_iocontrol(IOC_OSC_SET_ACTIVE, sbi->ll_md_exp,
2996                               sizeof *ioc_data, ioc_data, NULL);
2997
2998                 obd_iocontrol(IOC_OSC_SET_ACTIVE, sbi->ll_dt_exp,
2999                               sizeof *ioc_data, ioc_data, NULL);
3000
3001                 OBD_FREE_PTR(ioc_data);
3002         }
3003
3004         /* Really, we'd like to wait until there are no requests outstanding,
3005          * and then continue.  For now, we just periodically checking for vfs
3006          * to decrement mnt_cnt and hope to finish it within 10sec.
3007          */
3008         cnt = 10;
3009         while (cnt > 0 &&
3010                !may_umount(sbi->ll_mnt.mnt)) {
3011                 ssleep(1);
3012                 cnt -= 1;
3013         }
3014
3015         EXIT;
3016 }
3017
3018 int ll_remount_fs(struct super_block *sb, int *flags, char *data)
3019 {
3020         struct ll_sb_info *sbi = ll_s2sbi(sb);
3021         char *profilenm = get_profile_name(sb);
3022         int err;
3023         __u32 read_only;
3024
3025         if ((*flags & MS_RDONLY) != (sb->s_flags & SB_RDONLY)) {
3026                 read_only = *flags & MS_RDONLY;
3027                 err = obd_set_info_async(NULL, sbi->ll_md_exp,
3028                                          sizeof(KEY_READ_ONLY),
3029                                          KEY_READ_ONLY, sizeof(read_only),
3030                                          &read_only, NULL);
3031                 if (err) {
3032                         LCONSOLE_WARN("Failed to remount %s %s (%d)\n",
3033                                       profilenm, read_only ?
3034                                       "read-only" : "read-write", err);
3035                         return err;
3036                 }
3037
3038                 if (read_only)
3039                         sb->s_flags |= SB_RDONLY;
3040                 else
3041                         sb->s_flags &= ~SB_RDONLY;
3042
3043                 if (test_bit(LL_SBI_VERBOSE, sbi->ll_flags))
3044                         LCONSOLE_WARN("Remounted %s %s\n", profilenm,
3045                                       read_only ?  "read-only" : "read-write");
3046         }
3047         return 0;
3048 }
3049
3050 /**
3051  * Cleanup the open handle that is cached on MDT-side.
3052  *
3053  * For open case, the client side open handling thread may hit error
3054  * after the MDT grant the open. Under such case, the client should
3055  * send close RPC to the MDT as cleanup; otherwise, the open handle
3056  * on the MDT will be leaked there until the client umount or evicted.
3057  *
3058  * In further, if someone unlinked the file, because the open handle
3059  * holds the reference on such file/object, then it will block the
3060  * subsequent threads that want to locate such object via FID.
3061  *
3062  * \param[in] sb        super block for this file-system
3063  * \param[in] open_req  pointer to the original open request
3064  */
3065 void ll_open_cleanup(struct super_block *sb, struct req_capsule *pill)
3066 {
3067         struct mdt_body                 *body;
3068         struct md_op_data               *op_data;
3069         struct ptlrpc_request           *close_req = NULL;
3070         struct obd_export               *exp       = ll_s2sbi(sb)->ll_md_exp;
3071         ENTRY;
3072
3073         body = req_capsule_server_get(pill, &RMF_MDT_BODY);
3074         OBD_ALLOC_PTR(op_data);
3075         if (op_data == NULL) {
3076                 CWARN("%s: cannot allocate op_data to release open handle for "
3077                       DFID"\n", ll_s2sbi(sb)->ll_fsname, PFID(&body->mbo_fid1));
3078
3079                 RETURN_EXIT;
3080         }
3081
3082         op_data->op_fid1 = body->mbo_fid1;
3083         op_data->op_open_handle = body->mbo_open_handle;
3084         op_data->op_mod_time = ktime_get_real_seconds();
3085         md_close(exp, op_data, NULL, &close_req);
3086         ptlrpc_req_finished(close_req);
3087         ll_finish_md_op_data(op_data);
3088
3089         EXIT;
3090 }
3091
3092 /* set filesystem-wide default LMV for subdir mount if it's enabled on ROOT. */
3093 static int ll_fileset_default_lmv_fixup(struct inode *inode,
3094                                         struct lustre_md *md)
3095 {
3096         struct ll_sb_info *sbi = ll_i2sbi(inode);
3097         struct ptlrpc_request *req = NULL;
3098         union lmv_mds_md *lmm = NULL;
3099         int size = 0;
3100         int rc;
3101
3102         LASSERT(is_root_inode(inode));
3103         LASSERT(!fid_is_root(&sbi->ll_root_fid));
3104         LASSERT(!md->default_lmv);
3105
3106         rc = ll_dir_get_default_layout(inode, (void **)&lmm, &size, &req,
3107                                        OBD_MD_DEFAULT_MEA,
3108                                        GET_DEFAULT_LAYOUT_ROOT);
3109         if (rc && rc != -ENODATA)
3110                 GOTO(out, rc);
3111
3112         rc = 0;
3113         if (lmm && size) {
3114                 rc = md_unpackmd(sbi->ll_md_exp, &md->default_lmv, lmm, size);
3115                 if (rc < 0)
3116                         GOTO(out, rc);
3117
3118                 rc = 0;
3119         }
3120         EXIT;
3121 out:
3122         if (req)
3123                 ptlrpc_req_finished(req);
3124         return rc;
3125 }
3126
3127 int ll_prep_inode(struct inode **inode, struct req_capsule *pill,
3128                   struct super_block *sb, struct lookup_intent *it)
3129 {
3130         struct ll_sb_info *sbi = NULL;
3131         struct lustre_md md = { NULL };
3132         bool default_lmv_deleted = false;
3133         int rc;
3134
3135         ENTRY;
3136
3137         LASSERT(*inode || sb);
3138         sbi = sb ? ll_s2sbi(sb) : ll_i2sbi(*inode);
3139         rc = md_get_lustre_md(sbi->ll_md_exp, pill, sbi->ll_dt_exp,
3140                               sbi->ll_md_exp, &md);
3141         if (rc != 0)
3142                 GOTO(out, rc);
3143
3144         /*
3145          * clear default_lmv only if intent_getattr reply doesn't contain it.
3146          * but it needs to be done after iget, check this early because
3147          * ll_update_lsm_md() may change md.
3148          */
3149         if (it && (it->it_op & (IT_LOOKUP | IT_GETATTR)) &&
3150             S_ISDIR(md.body->mbo_mode) && !md.default_lmv) {
3151                 if (unlikely(*inode && is_root_inode(*inode) &&
3152                              !fid_is_root(&sbi->ll_root_fid))) {
3153                         rc = ll_fileset_default_lmv_fixup(*inode, &md);
3154                         if (rc)
3155                                 GOTO(out, rc);
3156                 }
3157
3158                 if (!md.default_lmv)
3159                         default_lmv_deleted = true;
3160         }
3161
3162         if (*inode) {
3163                 rc = ll_update_inode(*inode, &md);
3164                 if (rc != 0)
3165                         GOTO(out, rc);
3166         } else {
3167                 bool api32 = test_bit(LL_SBI_32BIT_API, sbi->ll_flags);
3168                 struct lu_fid *fid1 = &md.body->mbo_fid1;
3169
3170                 LASSERT(sb != NULL);
3171
3172                 /*
3173                  * At this point server returns to client's same fid as client
3174                  * generated for creating. So using ->fid1 is okay here.
3175                  */
3176                 if (!fid_is_sane(fid1)) {
3177                         CERROR("%s: Fid is insane "DFID"\n",
3178                                 sbi->ll_fsname, PFID(fid1));
3179                         GOTO(out, rc = -EINVAL);
3180                 }
3181
3182                 *inode = ll_iget(sb, cl_fid_build_ino(fid1, api32), &md);
3183                 if (IS_ERR(*inode)) {
3184                         lmd_clear_acl(&md);
3185                         rc = IS_ERR(*inode) ? PTR_ERR(*inode) : -ENOMEM;
3186                         *inode = NULL;
3187                         CERROR("new_inode -fatal: rc %d\n", rc);
3188                         GOTO(out, rc);
3189                 }
3190         }
3191
3192         /* Handling piggyback layout lock.
3193          * Layout lock can be piggybacked by getattr and open request.
3194          * The lsm can be applied to inode only if it comes with a layout lock
3195          * otherwise correct layout may be overwritten, for example:
3196          * 1. proc1: mdt returns a lsm but not granting layout
3197          * 2. layout was changed by another client
3198          * 3. proc2: refresh layout and layout lock granted
3199          * 4. proc1: to apply a stale layout */
3200         if (it != NULL && it->it_lock_mode != 0) {
3201                 struct lustre_handle lockh;
3202                 struct ldlm_lock *lock;
3203
3204                 lockh.cookie = it->it_lock_handle;
3205                 lock = ldlm_handle2lock(&lockh);
3206                 LASSERT(lock != NULL);
3207                 if (ldlm_has_layout(lock)) {
3208                         struct cl_object_conf conf;
3209
3210                         memset(&conf, 0, sizeof(conf));
3211                         conf.coc_opc = OBJECT_CONF_SET;
3212                         conf.coc_inode = *inode;
3213                         conf.coc_lock = lock;
3214                         conf.u.coc_layout = md.layout;
3215                         (void)ll_layout_conf(*inode, &conf);
3216                 }
3217                 LDLM_LOCK_PUT(lock);
3218         }
3219
3220         if (default_lmv_deleted)
3221                 ll_update_default_lsm_md(*inode, &md);
3222
3223         /* we may want to apply some policy for foreign file/dir */
3224         if (ll_sbi_has_foreign_symlink(sbi)) {
3225                 rc = ll_manage_foreign(*inode, &md);
3226                 if (rc < 0)
3227                         GOTO(out, rc);
3228         }
3229
3230         GOTO(out, rc = 0);
3231
3232 out:
3233         /* cleanup will be done if necessary */
3234         md_free_lustre_md(sbi->ll_md_exp, &md);
3235
3236         if (rc != 0 && it != NULL && it->it_op & IT_OPEN) {
3237                 ll_intent_drop_lock(it);
3238                 ll_open_cleanup(sb != NULL ? sb : (*inode)->i_sb, pill);
3239         }
3240
3241         return rc;
3242 }
3243
3244 int ll_obd_statfs(struct inode *inode, void __user *arg)
3245 {
3246         struct ll_sb_info *sbi = NULL;
3247         struct obd_export *exp;
3248         struct obd_ioctl_data *data = NULL;
3249         __u32 type;
3250         int len = 0, rc;
3251
3252         if (inode)
3253                 sbi = ll_i2sbi(inode);
3254         if (!sbi)
3255                 GOTO(out_statfs, rc = -EINVAL);
3256
3257         rc = obd_ioctl_getdata(&data, &len, arg);
3258         if (rc)
3259                 GOTO(out_statfs, rc);
3260
3261         if (!data->ioc_inlbuf1 || !data->ioc_inlbuf2 ||
3262             !data->ioc_pbuf1 || !data->ioc_pbuf2)
3263                 GOTO(out_statfs, rc = -EINVAL);
3264
3265         if (data->ioc_inllen1 != sizeof(__u32) ||
3266             data->ioc_inllen2 != sizeof(__u32) ||
3267             data->ioc_plen1 != sizeof(struct obd_statfs) ||
3268             data->ioc_plen2 != sizeof(struct obd_uuid))
3269                 GOTO(out_statfs, rc = -EINVAL);
3270
3271         memcpy(&type, data->ioc_inlbuf1, sizeof(__u32));
3272         if (type & LL_STATFS_LMV)
3273                 exp = sbi->ll_md_exp;
3274         else if (type & LL_STATFS_LOV)
3275                 exp = sbi->ll_dt_exp;
3276         else
3277                 GOTO(out_statfs, rc = -ENODEV);
3278
3279         rc = obd_iocontrol(IOC_OBD_STATFS, exp, len, data, NULL);
3280         if (rc)
3281                 GOTO(out_statfs, rc);
3282 out_statfs:
3283         OBD_FREE_LARGE(data, len);
3284         return rc;
3285 }
3286
3287 /*
3288  * this is normally called in ll_fini_md_op_data(), but sometimes it needs to
3289  * be called early to avoid deadlock.
3290  */
3291 void ll_unlock_md_op_lsm(struct md_op_data *op_data)
3292 {
3293         if (op_data->op_mea2_sem) {
3294                 up_read_non_owner(op_data->op_mea2_sem);
3295                 op_data->op_mea2_sem = NULL;
3296         }
3297
3298         if (op_data->op_mea1_sem) {
3299                 up_read_non_owner(op_data->op_mea1_sem);
3300                 op_data->op_mea1_sem = NULL;
3301         }
3302 }
3303
3304 /* this function prepares md_op_data hint for passing it down to MD stack. */
3305 struct md_op_data *ll_prep_md_op_data(struct md_op_data *op_data,
3306                                       struct inode *i1, struct inode *i2,
3307                                       const char *name, size_t namelen,
3308                                       __u32 mode, enum md_op_code opc,
3309                                       void *data)
3310 {
3311         struct llcrypt_name fname = { 0 };
3312         int rc;
3313
3314         LASSERT(i1 != NULL);
3315
3316         if (name == NULL) {
3317                 /* Do not reuse namelen for something else. */
3318                 if (namelen != 0)
3319                         return ERR_PTR(-EINVAL);
3320         } else {
3321                 if ((!IS_ENCRYPTED(i1) ||
3322                      (opc != LUSTRE_OPC_LOOKUP && opc != LUSTRE_OPC_CREATE)) &&
3323                     namelen > ll_i2sbi(i1)->ll_namelen)
3324                         return ERR_PTR(-ENAMETOOLONG);
3325
3326                 /* "/" is not valid name, but it's allowed */
3327                 if (!lu_name_is_valid_2(name, namelen) &&
3328                     strncmp("/", name, namelen) != 0)
3329                         return ERR_PTR(-EINVAL);
3330         }
3331
3332         if (op_data == NULL)
3333                 OBD_ALLOC_PTR(op_data);
3334
3335         if (op_data == NULL)
3336                 return ERR_PTR(-ENOMEM);
3337
3338         ll_i2gids(op_data->op_suppgids, i1, i2);
3339         /* If the client is using a subdir mount and looks at what it sees as
3340          * /.fscrypt, interpret it as the .fscrypt dir at the root of the fs.
3341          */
3342         if (unlikely(i1->i_sb && i1->i_sb->s_root && is_root_inode(i1) &&
3343                      !fid_is_root(ll_inode2fid(i1)) &&
3344                      name && namelen == strlen(dot_fscrypt_name) &&
3345                      strncmp(name, dot_fscrypt_name, namelen) == 0))
3346                 lu_root_fid(&op_data->op_fid1);
3347         else
3348                 op_data->op_fid1 = *ll_inode2fid(i1);
3349
3350         if (S_ISDIR(i1->i_mode)) {
3351                 down_read_non_owner(&ll_i2info(i1)->lli_lsm_sem);
3352                 op_data->op_mea1_sem = &ll_i2info(i1)->lli_lsm_sem;
3353                 op_data->op_mea1 = ll_i2info(i1)->lli_lsm_md;
3354                 op_data->op_default_mea1 = ll_i2info(i1)->lli_default_lsm_md;
3355         }
3356
3357         if (i2) {
3358                 op_data->op_fid2 = *ll_inode2fid(i2);
3359                 if (S_ISDIR(i2->i_mode)) {
3360                         if (i2 != i1) {
3361                                 /* i2 is typically a child of i1, and MUST be
3362                                  * further from the root to avoid deadlocks.
3363                                  */
3364                                 down_read_non_owner(&ll_i2info(i2)->lli_lsm_sem);
3365                                 op_data->op_mea2_sem =
3366                                                 &ll_i2info(i2)->lli_lsm_sem;
3367                         }
3368                         op_data->op_mea2 = ll_i2info(i2)->lli_lsm_md;
3369                 }
3370         } else {
3371                 fid_zero(&op_data->op_fid2);
3372         }
3373
3374         if (test_bit(LL_SBI_64BIT_HASH, ll_i2sbi(i1)->ll_flags))
3375                 op_data->op_cli_flags |= CLI_HASH64;
3376
3377         if (ll_need_32bit_api(ll_i2sbi(i1)))
3378                 op_data->op_cli_flags |= CLI_API32;
3379
3380         if ((i2 && is_root_inode(i2)) ||
3381             opc == LUSTRE_OPC_LOOKUP || opc == LUSTRE_OPC_CREATE) {
3382                 /* In case of lookup, ll_setup_filename() has already been
3383                  * called in ll_lookup_it(), so just take provided name.
3384                  * Also take provided name if we are dealing with root inode.
3385                  */
3386                 fname.disk_name.name = (unsigned char *)name;
3387                 fname.disk_name.len = namelen;
3388         } else if (name && namelen) {
3389                 struct qstr dname = QSTR_INIT(name, namelen);
3390                 struct inode *dir;
3391                 struct lu_fid *pfid = NULL;
3392                 struct lu_fid fid;
3393                 int lookup;
3394
3395                 if (!S_ISDIR(i1->i_mode) && i2 && S_ISDIR(i2->i_mode)) {
3396                         /* special case when called from ll_link() */
3397                         dir = i2;
3398                         lookup = 0;
3399                 } else {
3400                         dir = i1;
3401                         lookup = (int)(opc == LUSTRE_OPC_ANY);
3402                 }
3403                 if (opc == LUSTRE_OPC_ANY && lookup)
3404                         pfid = &fid;
3405                 rc = ll_setup_filename(dir, &dname, lookup, &fname, pfid);
3406                 if (rc) {
3407                         ll_finish_md_op_data(op_data);
3408                         return ERR_PTR(rc);
3409                 }
3410                 if (pfid && !fid_is_zero(pfid)) {
3411                         if (i2 == NULL)
3412                                 op_data->op_fid2 = fid;
3413                         op_data->op_bias = MDS_FID_OP;
3414                 }
3415                 if (fname.disk_name.name &&
3416                     fname.disk_name.name != (unsigned char *)name) {
3417                         /* op_data->op_name must be freed after use */
3418                         op_data->op_flags |= MF_OPNAME_KMALLOCED;
3419                 }
3420         }
3421
3422         /* In fact LUSTRE_OPC_LOOKUP, LUSTRE_OPC_OPEN
3423          * are LUSTRE_OPC_ANY
3424          */
3425         if (opc == LUSTRE_OPC_LOOKUP || opc == LUSTRE_OPC_OPEN)
3426                 op_data->op_code = LUSTRE_OPC_ANY;
3427         else
3428                 op_data->op_code = opc;
3429         op_data->op_name = fname.disk_name.name;
3430         op_data->op_namelen = fname.disk_name.len;
3431         op_data->op_mode = mode;
3432         op_data->op_mod_time = ktime_get_real_seconds();
3433         op_data->op_fsuid = from_kuid(&init_user_ns, current_fsuid());
3434         op_data->op_fsgid = from_kgid(&init_user_ns, current_fsgid());
3435         op_data->op_cap = current_cap();
3436         op_data->op_mds = 0;
3437         if ((opc == LUSTRE_OPC_CREATE) && (name != NULL) &&
3438              filename_is_volatile(name, namelen, &op_data->op_mds)) {
3439                 op_data->op_bias |= MDS_CREATE_VOLATILE;
3440         }
3441         op_data->op_data = data;
3442
3443         return op_data;
3444 }
3445
3446 void ll_finish_md_op_data(struct md_op_data *op_data)
3447 {
3448         ll_unlock_md_op_lsm(op_data);
3449         ll_security_release_secctx(op_data->op_file_secctx,
3450                                    op_data->op_file_secctx_size);
3451         if (op_data->op_flags & MF_OPNAME_KMALLOCED)
3452                 /* allocated via ll_setup_filename called
3453                  * from ll_prep_md_op_data
3454                  */
3455                 kfree(op_data->op_name);
3456         llcrypt_free_ctx(op_data->op_file_encctx, op_data->op_file_encctx_size);
3457         OBD_FREE_PTR(op_data);
3458 }
3459
3460 int ll_show_options(struct seq_file *seq, struct dentry *dentry)
3461 {
3462         struct ll_sb_info *sbi;
3463         int i;
3464
3465         LASSERT(seq && dentry);
3466         sbi = ll_s2sbi(dentry->d_sb);
3467
3468         if (test_bit(LL_SBI_NOLCK, sbi->ll_flags))
3469                 seq_puts(seq, "nolock");
3470
3471         for (i = 1; ll_sbi_flags_name[i].token != LL_SBI_NUM_MOUNT_OPT; i++) {
3472                 /* match_table in some cases has patterns for both enabled and
3473                  * disabled cases. Ignore 'no'xxx versions if bit is set.
3474                  */
3475                 if (test_bit(ll_sbi_flags_name[i].token, sbi->ll_flags) &&
3476                     strncmp(ll_sbi_flags_name[i].pattern, "no", 2)) {
3477                         if (ll_sbi_flags_name[i].token ==
3478                             LL_SBI_FOREIGN_SYMLINK) {
3479                                 seq_show_option(seq, "foreign_symlink",
3480                                                 sbi->ll_foreign_symlink_prefix);
3481                         } else {
3482                                 seq_printf(seq, ",%s",
3483                                            ll_sbi_flags_name[i].pattern);
3484                         }
3485
3486                         /* You can have either localflock or flock but not
3487                          * both. If localflock is set don't print flock or
3488                          * noflock.
3489                          */
3490                         if (ll_sbi_flags_name[i].token == LL_SBI_LOCALFLOCK)
3491                                 i += 2;
3492                 } else if (!test_bit(ll_sbi_flags_name[i].token, sbi->ll_flags) &&
3493                            !strncmp(ll_sbi_flags_name[i].pattern, "no", 2)) {
3494                         seq_printf(seq, ",%s",
3495                                    ll_sbi_flags_name[i].pattern);
3496                 }
3497         }
3498
3499         llcrypt_show_test_dummy_encryption(seq, ',', dentry->d_sb);
3500
3501         RETURN(0);
3502 }
3503
3504 /**
3505  * Get obd name by cmd, and copy out to user space
3506  */
3507 int ll_get_obd_name(struct inode *inode, unsigned int cmd, unsigned long arg)
3508 {
3509         struct ll_sb_info *sbi = ll_i2sbi(inode);
3510         struct obd_device *obd;
3511         ENTRY;
3512
3513         if (cmd == OBD_IOC_GETNAME_OLD || cmd == OBD_IOC_GETDTNAME)
3514                 obd = class_exp2obd(sbi->ll_dt_exp);
3515         else if (cmd == OBD_IOC_GETMDNAME)
3516                 obd = class_exp2obd(sbi->ll_md_exp);
3517         else
3518                 RETURN(-EINVAL);
3519
3520         if (!obd)
3521                 RETURN(-ENOENT);
3522
3523         if (copy_to_user((void __user *)arg, obd->obd_name,
3524                          strlen(obd->obd_name) + 1))
3525                 RETURN(-EFAULT);
3526
3527         RETURN(0);
3528 }
3529
3530 struct dname_buf {
3531         struct work_struct db_work;
3532         struct dentry *db_dentry;
3533         /* Let's hope the path is not too long, 32 bytes for the work struct
3534          * on my kernel
3535          */
3536         char buf[PAGE_SIZE - sizeof(struct work_struct) - sizeof(void *)];
3537 };
3538
3539 static void ll_dput_later(struct work_struct *work)
3540 {
3541         struct dname_buf *db = container_of(work, struct dname_buf, db_work);
3542
3543         dput(db->db_dentry);
3544         free_page((unsigned long)db);
3545 }
3546
3547 static char* ll_d_path(struct dentry *dentry, char *buf, int bufsize)
3548 {
3549         char *path = NULL;
3550
3551         struct path p;
3552
3553         p.dentry = dentry;
3554         p.mnt = current->fs->root.mnt;
3555         path_get(&p);
3556         path = d_path(&p, buf, bufsize);
3557         path_put(&p);
3558         return path;
3559 }
3560
3561 void ll_dirty_page_discard_warn(struct inode *inode, int ioret)
3562 {
3563         struct dname_buf *db;
3564         char  *path = NULL;
3565         struct dentry *dentry = NULL;
3566
3567         /* this can be called inside spin lock so use GFP_ATOMIC. */
3568         db = (struct dname_buf *)__get_free_page(GFP_ATOMIC);
3569         if (db != NULL) {
3570
3571                 dentry = d_find_alias(inode);
3572                 if (dentry != NULL)
3573                         path = ll_d_path(dentry, db->buf, sizeof(db->buf));
3574         }
3575
3576         /* The below message is checked in recovery-small.sh test_24b */
3577         CDEBUG(D_WARNING,
3578                "%s: dirty page discard: %s/fid: "DFID"/%s may get corrupted "
3579                "(rc %d)\n", ll_i2sbi(inode)->ll_fsname,
3580                s2lsi(inode->i_sb)->lsi_lmd->lmd_dev,
3581                PFID(ll_inode2fid(inode)),
3582                (path && !IS_ERR(path)) ? path : "", ioret);
3583
3584         if (dentry != NULL) {
3585                 /* We cannot dput here since if we happen to be the last holder
3586                  * then we can end up waiting for page evictions that
3587                  * in turn wait for RPCs that need this instance of ptlrpcd
3588                  * (callng brw_interpret->*page_completion*->vmpage_error->here)
3589                  * LU-15340
3590                  */
3591                 INIT_WORK(&db->db_work, ll_dput_later);
3592                 db->db_dentry = dentry;
3593                 schedule_work(&db->db_work);
3594         } else {
3595                 if (db != NULL)
3596                         free_page((unsigned long)db);
3597         }
3598 }
3599
3600 ssize_t ll_copy_user_md(const struct lov_user_md __user *md,
3601                         struct lov_user_md **kbuf)
3602 {
3603         struct lov_user_md      lum;
3604         ssize_t                 lum_size;
3605         ENTRY;
3606
3607         if (copy_from_user(&lum, md, sizeof(lum)))
3608                 RETURN(-EFAULT);
3609
3610         lum_size = ll_lov_user_md_size(&lum);
3611         if (lum_size < 0)
3612                 RETURN(lum_size);
3613
3614         OBD_ALLOC_LARGE(*kbuf, lum_size);
3615         if (*kbuf == NULL)
3616                 RETURN(-ENOMEM);
3617
3618         if (copy_from_user(*kbuf, md, lum_size) != 0) {
3619                 OBD_FREE_LARGE(*kbuf, lum_size);
3620                 RETURN(-EFAULT);
3621         }
3622
3623         RETURN(lum_size);
3624 }
3625
3626 /*
3627  * Compute llite root squash state after a change of root squash
3628  * configuration setting or add/remove of a lnet nid
3629  */
3630 void ll_compute_rootsquash_state(struct ll_sb_info *sbi)
3631 {
3632         struct root_squash_info *squash = &sbi->ll_squash;
3633         int i;
3634         bool matched;
3635         struct lnet_processid id;
3636
3637         /* Update norootsquash flag */
3638         spin_lock(&squash->rsi_lock);
3639         if (list_empty(&squash->rsi_nosquash_nids))
3640                 clear_bit(LL_SBI_NOROOTSQUASH, sbi->ll_flags);
3641         else {
3642                 /* Do not apply root squash as soon as one of our NIDs is
3643                  * in the nosquash_nids list */
3644                 matched = false;
3645                 i = 0;
3646                 while (LNetGetId(i++, &id) != -ENOENT) {
3647                         if (nid_is_lo0(&id.nid))
3648                                 continue;
3649                         if (cfs_match_nid(lnet_nid_to_nid4(&id.nid),
3650                                           &squash->rsi_nosquash_nids)) {
3651                                 matched = true;
3652                                 break;
3653                         }
3654                 }
3655                 if (matched)
3656                         set_bit(LL_SBI_NOROOTSQUASH, sbi->ll_flags);
3657                 else
3658                         clear_bit(LL_SBI_NOROOTSQUASH, sbi->ll_flags);
3659         }
3660         spin_unlock(&squash->rsi_lock);
3661 }
3662
3663 /**
3664  * Parse linkea content to extract information about a given hardlink
3665  *
3666  * \param[in]   ldata      - Initialized linkea data
3667  * \param[in]   linkno     - Link identifier
3668  * \param[out]  parent_fid - The entry's parent FID
3669  * \param[out]  ln         - Entry name destination buffer
3670  *
3671  * \retval 0 on success
3672  * \retval Appropriate negative error code on failure
3673  */
3674 static int ll_linkea_decode(struct linkea_data *ldata, unsigned int linkno,
3675                             struct lu_fid *parent_fid, struct lu_name *ln)
3676 {
3677         unsigned int    idx;
3678         int             rc;
3679         ENTRY;
3680
3681         rc = linkea_init_with_rec(ldata);
3682         if (rc < 0)
3683                 RETURN(rc);
3684
3685         if (linkno >= ldata->ld_leh->leh_reccount)
3686                 /* beyond last link */
3687                 RETURN(-ENODATA);
3688
3689         linkea_first_entry(ldata);
3690         for (idx = 0; ldata->ld_lee != NULL; idx++) {
3691                 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen, ln,
3692                                     parent_fid);
3693                 if (idx == linkno)
3694                         break;
3695
3696                 linkea_next_entry(ldata);
3697         }
3698
3699         if (idx < linkno)
3700                 RETURN(-ENODATA);
3701
3702         RETURN(0);
3703 }
3704
3705 /**
3706  * Get parent FID and name of an identified link. Operation is performed for
3707  * a given link number, letting the caller iterate over linkno to list one or
3708  * all links of an entry.
3709  *
3710  * \param[in]     file - File descriptor against which to perform the operation
3711  * \param[in,out] arg  - User-filled structure containing the linkno to operate
3712  *                       on and the available size. It is eventually filled with
3713  *                       the requested information or left untouched on error
3714  *
3715  * \retval - 0 on success
3716  * \retval - Appropriate negative error code on failure
3717  */
3718 int ll_getparent(struct file *file, struct getparent __user *arg)
3719 {
3720         struct inode            *inode = file_inode(file);
3721         struct linkea_data      *ldata;
3722         struct lu_buf            buf = LU_BUF_NULL;
3723         struct lu_name           ln;
3724         struct lu_fid            parent_fid;
3725         __u32                    linkno;
3726         __u32                    name_size;
3727         int                      rc;
3728
3729         ENTRY;
3730
3731         if (!capable(CAP_DAC_READ_SEARCH) &&
3732             !test_bit(LL_SBI_USER_FID2PATH, ll_i2sbi(inode)->ll_flags))
3733                 RETURN(-EPERM);
3734
3735         if (get_user(name_size, &arg->gp_name_size))
3736                 RETURN(-EFAULT);
3737
3738         if (get_user(linkno, &arg->gp_linkno))
3739                 RETURN(-EFAULT);
3740
3741         if (name_size > PATH_MAX)
3742                 RETURN(-EINVAL);
3743
3744         OBD_ALLOC(ldata, sizeof(*ldata));
3745         if (ldata == NULL)
3746                 RETURN(-ENOMEM);
3747
3748         rc = linkea_data_new(ldata, &buf);
3749         if (rc < 0)
3750                 GOTO(ldata_free, rc);
3751
3752         rc = ll_xattr_list(inode, XATTR_NAME_LINK, XATTR_TRUSTED_T, buf.lb_buf,
3753                            buf.lb_len, OBD_MD_FLXATTR);
3754         if (rc < 0)
3755                 GOTO(lb_free, rc);
3756
3757         rc = ll_linkea_decode(ldata, linkno, &parent_fid, &ln);
3758         if (rc < 0)
3759                 GOTO(lb_free, rc);
3760
3761         if (ln.ln_namelen >= name_size)
3762                 GOTO(lb_free, rc = -EOVERFLOW);
3763
3764         if (copy_to_user(&arg->gp_fid, &parent_fid, sizeof(arg->gp_fid)))
3765                 GOTO(lb_free, rc = -EFAULT);
3766
3767         if (copy_to_user(&arg->gp_name, ln.ln_name, ln.ln_namelen))
3768                 GOTO(lb_free, rc = -EFAULT);
3769
3770         if (put_user('\0', arg->gp_name + ln.ln_namelen))
3771                 GOTO(lb_free, rc = -EFAULT);
3772
3773 lb_free:
3774         lu_buf_free(&buf);
3775 ldata_free:
3776         OBD_FREE(ldata, sizeof(*ldata));
3777
3778         RETURN(rc);
3779 }