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