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