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