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