Whamcloud - gitweb
LU-17744 ldiskfs: mballoc stats fixes
[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_opendir_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                 LASSERT(lli->lli_opendir_pid == 0);
1932         } else {
1933                 pcc_inode_free(inode);
1934         }
1935
1936         md_null_inode(sbi->ll_md_exp, ll_inode2fid(inode));
1937
1938         LASSERT(!lli->lli_open_fd_write_count);
1939         LASSERT(!lli->lli_open_fd_read_count);
1940         LASSERT(!lli->lli_open_fd_exec_count);
1941
1942         if (lli->lli_mds_write_och)
1943                 ll_md_real_close(inode, FMODE_WRITE);
1944         if (lli->lli_mds_exec_och)
1945                 ll_md_real_close(inode, FMODE_EXEC);
1946         if (lli->lli_mds_read_och)
1947                 ll_md_real_close(inode, FMODE_READ);
1948
1949         if (S_ISLNK(inode->i_mode) && lli->lli_symlink_name) {
1950                 OBD_FREE(lli->lli_symlink_name,
1951                          strlen(lli->lli_symlink_name) + 1);
1952                 lli->lli_symlink_name = NULL;
1953         }
1954
1955         ll_xattr_cache_destroy(inode);
1956
1957         forget_all_cached_acls(inode);
1958         lli_clear_acl(lli);
1959         lli->lli_inode_magic = LLI_INODE_DEAD;
1960
1961         if (S_ISDIR(inode->i_mode))
1962                 ll_dir_clear_lsm_md(inode);
1963         else if (S_ISREG(inode->i_mode) && !is_bad_inode(inode))
1964                 LASSERT(list_empty(&lli->lli_agl_list));
1965
1966         /*
1967          * XXX This has to be done before lsm is freed below, because
1968          * cl_object still uses inode lsm.
1969          */
1970         cl_inode_fini(inode);
1971
1972         llcrypt_put_encryption_info(inode);
1973
1974         EXIT;
1975 }
1976
1977 static int ll_md_setattr(struct dentry *dentry, struct md_op_data *op_data)
1978 {
1979         struct lustre_md md;
1980         struct inode *inode = dentry->d_inode;
1981         struct ll_sb_info *sbi = ll_i2sbi(inode);
1982         struct ptlrpc_request *request = NULL;
1983         int rc, ia_valid;
1984
1985         ENTRY;
1986
1987         op_data = ll_prep_md_op_data(op_data, inode, NULL, NULL, 0, 0,
1988                                      LUSTRE_OPC_ANY, NULL);
1989         if (IS_ERR(op_data))
1990                 RETURN(PTR_ERR(op_data));
1991
1992         /* If this is a chgrp of a regular file, we want to reserve enough
1993          * quota to cover the entire file size.
1994          */
1995         if (S_ISREG(inode->i_mode) && op_data->op_attr.ia_valid & ATTR_GID &&
1996             from_kgid(&init_user_ns, op_data->op_attr.ia_gid) !=
1997             from_kgid(&init_user_ns, inode->i_gid)) {
1998                 op_data->op_xvalid |= OP_XVALID_BLOCKS;
1999                 op_data->op_attr_blocks = inode->i_blocks;
2000         }
2001
2002
2003         rc = md_setattr(sbi->ll_md_exp, op_data, NULL, 0, &request);
2004         if (rc) {
2005                 ptlrpc_req_finished(request);
2006                 if (rc == -ENOENT) {
2007                         clear_nlink(inode);
2008                         /* Unlinked special device node? Or just a race?
2009                          * Pretend we done everything. */
2010                         if (!S_ISREG(inode->i_mode) &&
2011                             !S_ISDIR(inode->i_mode)) {
2012                                 ia_valid = op_data->op_attr.ia_valid;
2013                                 op_data->op_attr.ia_valid &= ~TIMES_SET_FLAGS;
2014                                 rc = simple_setattr(&nop_mnt_idmap, dentry,
2015                                                     &op_data->op_attr);
2016                                 op_data->op_attr.ia_valid = ia_valid;
2017                         }
2018                 } else if (rc != -EPERM && rc != -EACCES && rc != -ETXTBSY) {
2019                         CERROR("md_setattr fails: rc = %d\n", rc);
2020                 }
2021                 RETURN(rc);
2022         }
2023
2024         rc = md_get_lustre_md(sbi->ll_md_exp, &request->rq_pill, sbi->ll_dt_exp,
2025                               sbi->ll_md_exp, &md);
2026         if (rc) {
2027                 ptlrpc_req_finished(request);
2028                 RETURN(rc);
2029         }
2030
2031         ia_valid = op_data->op_attr.ia_valid;
2032         /* inode size will be in ll_setattr_ost, can't do it now since dirty
2033          * cache is not cleared yet. */
2034         op_data->op_attr.ia_valid &= ~(TIMES_SET_FLAGS | ATTR_SIZE);
2035         if (S_ISREG(inode->i_mode))
2036                 ll_inode_lock(inode);
2037         rc = simple_setattr(&nop_mnt_idmap, dentry, &op_data->op_attr);
2038         if (S_ISREG(inode->i_mode))
2039                 ll_inode_unlock(inode);
2040         op_data->op_attr.ia_valid = ia_valid;
2041
2042         rc = ll_update_inode(inode, &md);
2043         ptlrpc_req_finished(request);
2044         md_put_lustre_md(sbi->ll_md_exp, &md);
2045
2046         RETURN(rc);
2047 }
2048
2049 /**
2050  * Zero portion of page that is part of @inode.
2051  * This implies, if necessary:
2052  * - taking cl_lock on range corresponding to concerned page
2053  * - grabbing vm page
2054  * - associating cl_page
2055  * - proceeding to clio read
2056  * - zeroing range in page
2057  * - proceeding to cl_page flush
2058  * - releasing cl_lock
2059  *
2060  * \param[in] inode     inode
2061  * \param[in] index     page index
2062  * \param[in] offset    offset in page to start zero from
2063  * \param[in] len       len to zero
2064  *
2065  * \retval 0            on success
2066  * \retval negative     errno on failure
2067  */
2068 static int ll_io_zero_page(struct inode *inode, pgoff_t index, pgoff_t offset,
2069                            unsigned int len)
2070 {
2071         struct ll_inode_info *lli = ll_i2info(inode);
2072         struct cl_object *clob = lli->lli_clob;
2073         __u16 refcheck;
2074         struct lu_env *env = NULL;
2075         struct cl_io *io = NULL;
2076         struct cl_page *clpage = NULL;
2077         struct page *vmpage = NULL;
2078         unsigned from = index << PAGE_SHIFT;
2079         struct cl_lock *lock = NULL;
2080         struct cl_lock_descr *descr = NULL;
2081         struct cl_2queue *queue = NULL;
2082         struct cl_sync_io *anchor = NULL;
2083         bool holdinglock = false;
2084         int rc;
2085
2086         ENTRY;
2087
2088         env = cl_env_get(&refcheck);
2089         if (IS_ERR(env))
2090                 RETURN(PTR_ERR(env));
2091
2092         io = vvp_env_thread_io(env);
2093         io->ci_obj = clob;
2094         rc = cl_io_rw_init(env, io, CIT_WRITE, from, PAGE_SIZE);
2095         if (rc)
2096                 GOTO(putenv, rc);
2097
2098         lock = vvp_env_lock(env);
2099         descr = &lock->cll_descr;
2100         descr->cld_obj   = io->ci_obj;
2101         descr->cld_start = from >> PAGE_SHIFT;
2102         descr->cld_end   = (from + PAGE_SIZE - 1) >> PAGE_SHIFT;
2103         descr->cld_mode  = CLM_WRITE;
2104         descr->cld_enq_flags = CEF_MUST | CEF_NONBLOCK;
2105
2106         /* request lock for page */
2107         rc = cl_lock_request(env, io, lock);
2108         /* -ECANCELED indicates a matching lock with a different extent
2109          * was already present, and -EEXIST indicates a matching lock
2110          * on exactly the same extent was already present.
2111          * In both cases it means we are covered.
2112          */
2113         if (rc == -ECANCELED || rc == -EEXIST)
2114                 rc = 0;
2115         else if (rc < 0)
2116                 GOTO(iofini, rc);
2117         else
2118                 holdinglock = true;
2119
2120         /* grab page */
2121         vmpage = grab_cache_page_nowait(inode->i_mapping, index);
2122         if (vmpage == NULL)
2123                 GOTO(rellock, rc = -EOPNOTSUPP);
2124
2125         if (!PageDirty(vmpage)) {
2126                 /* associate cl_page */
2127                 clpage = cl_page_find(env, clob, vmpage->index,
2128                                       vmpage, CPT_CACHEABLE);
2129                 if (IS_ERR(clpage))
2130                         GOTO(pagefini, rc = PTR_ERR(clpage));
2131
2132                 cl_page_assume(env, io, clpage);
2133         }
2134
2135         if (!PageUptodate(vmpage) && !PageDirty(vmpage) &&
2136             !PageWriteback(vmpage)) {
2137                 /* read page */
2138                 /* Set PagePrivate2 to detect special case of empty page
2139                  * in osc_brw_fini_request().
2140                  * It is also used to tell ll_io_read_page() that we do not
2141                  * want the vmpage to be unlocked.
2142                  */
2143                 SetPagePrivate2(vmpage);
2144                 rc = ll_io_read_page(env, io, clpage, NULL);
2145                 if (!PagePrivate2(vmpage)) {
2146                         /* PagePrivate2 was cleared in osc_brw_fini_request()
2147                          * meaning we read an empty page. In this case, in order
2148                          * to avoid allocating unnecessary block in truncated
2149                          * file, we must not zero and write as below. Subsequent
2150                          * server-side truncate will handle things correctly.
2151                          */
2152                         cl_page_unassume(env, io, clpage);
2153                         GOTO(clpfini, rc = 0);
2154                 }
2155                 ClearPagePrivate2(vmpage);
2156                 if (rc)
2157                         GOTO(clpfini, rc);
2158         }
2159
2160         /* Thanks to PagePrivate2 flag, ll_io_read_page() did not unlock
2161          * the vmpage, so we are good to proceed and zero range in page.
2162          */
2163         zero_user(vmpage, offset, len);
2164
2165         if (holdinglock && clpage) {
2166                 /* explicitly write newly modified page */
2167                 queue = &io->ci_queue;
2168                 cl_2queue_init(queue);
2169                 anchor = &vvp_env_info(env)->vti_anchor;
2170                 cl_sync_io_init(anchor, 1);
2171                 clpage->cp_sync_io = anchor;
2172                 cl_page_list_add(&queue->c2_qin, clpage, true);
2173                 rc = cl_io_submit_rw(env, io, CRT_WRITE, queue);
2174                 if (rc)
2175                         GOTO(queuefini1, rc);
2176                 rc = cl_sync_io_wait(env, anchor, 0);
2177                 if (rc)
2178                         GOTO(queuefini2, rc);
2179                 cl_page_assume(env, io, clpage);
2180
2181 queuefini2:
2182                 cl_2queue_discard(env, io, queue);
2183 queuefini1:
2184                 cl_2queue_disown(env, queue);
2185                 cl_2queue_fini(env, queue);
2186         }
2187
2188 clpfini:
2189         if (clpage)
2190                 cl_page_put(env, clpage);
2191 pagefini:
2192         unlock_page(vmpage);
2193         put_page(vmpage);
2194 rellock:
2195         if (holdinglock)
2196                 cl_lock_release(env, lock);
2197 iofini:
2198         cl_io_fini(env, io);
2199 putenv:
2200         if (env)
2201                 cl_env_put(env, &refcheck);
2202
2203         RETURN(rc);
2204 }
2205
2206 /**
2207  * Get reference file from volatile file name.
2208  * Volatile file name may look like:
2209  * <parent>/LUSTRE_VOLATILE_HDR:<mdt_index>:<random>:fd=<fd>
2210  * where fd is opened descriptor of reference file.
2211  *
2212  * \param[in] volatile_name     volatile file name
2213  * \param[in] volatile_len      volatile file name length
2214  * \param[out] ref_file         pointer to struct file of reference file
2215  *
2216  * \retval 0            on success
2217  * \retval negative     errno on failure
2218  */
2219 int volatile_ref_file(const char *volatile_name, int volatile_len,
2220                       struct file **ref_file)
2221 {
2222         char *p, *q, *fd_str;
2223         int fd, rc;
2224
2225         p = strnstr(volatile_name, ":fd=", volatile_len);
2226         if (!p || strlen(p + 4) == 0)
2227                 return -EINVAL;
2228
2229         q = strchrnul(p + 4, ':');
2230         fd_str = kstrndup(p + 4, q - p - 4, GFP_NOFS);
2231         if (!fd_str)
2232                 return -ENOMEM;
2233         rc = kstrtouint(fd_str, 10, &fd);
2234         kfree(fd_str);
2235         if (rc)
2236                 return -EINVAL;
2237
2238         *ref_file = fget(fd);
2239         if (!(*ref_file))
2240                 return -EINVAL;
2241         return 0;
2242 }
2243
2244 /* If this inode has objects allocated to it (lsm != NULL), then the OST
2245  * object(s) determine the file size and mtime.  Otherwise, the MDS will
2246  * keep these values until such a time that objects are allocated for it.
2247  * We do the MDS operations first, as it is checking permissions for us.
2248  * We don't to the MDS RPC if there is nothing that we want to store there,
2249  * otherwise there is no harm in updating mtime/atime on the MDS if we are
2250  * going to do an RPC anyways.
2251  *
2252  * If we are doing a truncate, we will send the mtime and ctime updates
2253  * to the OST with the punch RPC, otherwise we do an explicit setattr RPC.
2254  * I don't believe it is possible to get e.g. ATTR_MTIME_SET and ATTR_SIZE
2255  * at the same time.
2256  *
2257  * In case of HSMimport, we only set attr on MDS.
2258  */
2259 int ll_setattr_raw(struct dentry *dentry, struct iattr *attr,
2260                    enum op_xvalid xvalid, bool hsm_import)
2261 {
2262         struct inode *inode = dentry->d_inode;
2263         struct ll_inode_info *lli = ll_i2info(inode);
2264         struct md_op_data *op_data = NULL;
2265         ktime_t kstart = ktime_get();
2266         int rc = 0;
2267
2268         ENTRY;
2269
2270         /* VFS has locked the inode before calling this */
2271         ll_set_inode_lock_owner(inode);
2272
2273         CDEBUG(D_VFSTRACE|D_IOTRACE,
2274                "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",
2275                dentry->d_name.name, PFID(ll_inode2fid(inode)), inode,
2276                i_size_read(inode), attr->ia_valid, attr->ia_mode,
2277                attr->ia_uid.val, attr->ia_gid.val, attr->ia_size,
2278                (long long) attr->ia_atime.tv_sec, attr->ia_atime.tv_nsec,
2279                (long long) attr->ia_mtime.tv_sec, attr->ia_mtime.tv_nsec,
2280                (long long) attr->ia_ctime.tv_sec, attr->ia_ctime.tv_nsec);
2281
2282
2283         if (attr->ia_valid & ATTR_SIZE) {
2284                 /* Check new size against VFS/VM file size limit and rlimit */
2285                 rc = inode_newsize_ok(inode, attr->ia_size);
2286                 if (rc)
2287                         GOTO(clear, rc);
2288
2289                 /* The maximum Lustre file size is variable, based on the
2290                  * OST maximum object size and number of stripes.  This
2291                  * needs another check in addition to the VFS check above. */
2292                 if (attr->ia_size > ll_file_maxbytes(inode)) {
2293                         CDEBUG(D_INODE,"file "DFID" too large %llu > %llu\n",
2294                                PFID(&lli->lli_fid), attr->ia_size,
2295                                ll_file_maxbytes(inode));
2296                         GOTO(clear, rc = -EFBIG);
2297                 }
2298
2299                 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
2300         }
2301
2302         /* POSIX: check before ATTR_*TIME_SET set (from inode_change_ok) */
2303         if (attr->ia_valid & TIMES_SET_FLAGS) {
2304                 if ((!uid_eq(current_fsuid(), inode->i_uid)) &&
2305                     !capable(CAP_FOWNER))
2306                         GOTO(clear, rc = -EPERM);
2307         }
2308
2309         /* We mark all of the fields "set" so MDS/OST does not re-set them */
2310         if (!(xvalid & OP_XVALID_CTIME_SET) &&
2311              (attr->ia_valid & ATTR_CTIME)) {
2312                 attr->ia_ctime = current_time(inode);
2313                 xvalid |= OP_XVALID_CTIME_SET;
2314         }
2315         if (!(attr->ia_valid & ATTR_ATIME_SET) &&
2316             (attr->ia_valid & ATTR_ATIME)) {
2317                 attr->ia_atime = current_time(inode);
2318                 attr->ia_valid |= ATTR_ATIME_SET;
2319         }
2320         if (!(attr->ia_valid & ATTR_MTIME_SET) &&
2321             (attr->ia_valid & ATTR_MTIME)) {
2322                 attr->ia_mtime = current_time(inode);
2323                 attr->ia_valid |= ATTR_MTIME_SET;
2324         }
2325
2326         if (attr->ia_valid & (ATTR_MTIME | ATTR_CTIME))
2327                 CDEBUG(D_INODE, "setting mtime %lld, ctime %lld, now = %lld\n",
2328                        (s64)attr->ia_mtime.tv_sec, (s64)attr->ia_ctime.tv_sec,
2329                        ktime_get_real_seconds());
2330
2331         if (S_ISREG(inode->i_mode))
2332                 ll_inode_unlock(inode);
2333
2334         /* We always do an MDS RPC, even if we're only changing the size;
2335          * only the MDS knows whether truncate() should fail with -ETXTBUSY */
2336
2337         OBD_ALLOC_PTR(op_data);
2338         if (op_data == NULL)
2339                 GOTO(out, rc = -ENOMEM);
2340
2341         if (!hsm_import && attr->ia_valid & ATTR_SIZE) {
2342                 /* If we are changing file size, file content is
2343                  * modified, flag it.
2344                  */
2345                 xvalid |= OP_XVALID_OWNEROVERRIDE;
2346                 op_data->op_bias |= MDS_DATA_MODIFIED;
2347                 clear_bit(LLIF_DATA_MODIFIED, &lli->lli_flags);
2348         }
2349
2350         if (attr->ia_valid & ATTR_FILE) {
2351                 struct ll_file_data *fd = attr->ia_file->private_data;
2352
2353                 if (fd->fd_lease_och)
2354                         op_data->op_bias |= MDS_TRUNC_KEEP_LEASE;
2355         }
2356
2357         op_data->op_attr = *attr;
2358         op_data->op_xvalid = xvalid;
2359
2360         rc = ll_md_setattr(dentry, op_data);
2361         if (rc)
2362                 GOTO(out, rc);
2363         lli->lli_synced_to_mds = false;
2364
2365         if (!S_ISREG(inode->i_mode) || hsm_import)
2366                 GOTO(out, rc = 0);
2367
2368         if (attr->ia_valid & (ATTR_SIZE | ATTR_ATIME | ATTR_ATIME_SET |
2369                               ATTR_MTIME | ATTR_MTIME_SET | ATTR_CTIME) ||
2370             xvalid & OP_XVALID_CTIME_SET) {
2371                 bool cached = false;
2372
2373                 rc = pcc_inode_setattr(inode, attr, &cached);
2374                 if (cached) {
2375                         if (rc) {
2376                                 CERROR("%s: PCC inode "DFID" setattr failed: "
2377                                        "rc = %d\n",
2378                                        ll_i2sbi(inode)->ll_fsname,
2379                                        PFID(&lli->lli_fid), rc);
2380                                 GOTO(out, rc);
2381                         }
2382                 } else {
2383                         unsigned int flags = 0;
2384
2385                         /* For truncate and utimes sending attributes to OSTs,
2386                          * setting mtime/atime to the past will be performed
2387                          * under PW [0:EOF] extent lock (new_size:EOF for
2388                          * truncate). It may seem excessive to send mtime/atime
2389                          * updates to OSTs when not setting times to past, but
2390                          * it is necessary due to possible time
2391                          * de-synchronization between MDT inode and OST objects
2392                          */
2393                         if (S_ISREG(inode->i_mode) && IS_ENCRYPTED(inode)) {
2394                                 xvalid |= OP_XVALID_FLAGS;
2395                                 flags = LUSTRE_ENCRYPT_FL;
2396                                 /* Call to ll_io_zero_page is not necessary if
2397                                  * truncating on PAGE_SIZE boundary, because
2398                                  * whole pages will be wiped.
2399                                  *
2400                                  * an exception is when working with the
2401                                  * raw ciphertext - in that case, we need to
2402                                  * encrypt those zeroes
2403                                  */
2404                                 if (attr->ia_valid & ATTR_SIZE &&
2405                                     attr->ia_size & ~PAGE_MASK &&
2406                                     !(attr->ia_valid & ATTR_FILE &&
2407                                       attr->ia_file->f_flags & O_CIPHERTEXT)) {
2408                                         pgoff_t offset =
2409                                                 attr->ia_size & (PAGE_SIZE - 1);
2410
2411                                         rc = ll_io_zero_page(inode,
2412                                                     attr->ia_size >> PAGE_SHIFT,
2413                                                     offset, PAGE_SIZE - offset);
2414                                         if (rc)
2415                                                 GOTO(out, rc);
2416                                 }
2417                                 /* If encrypted volatile file without the key,
2418                                  * we need to fetch size from reference file,
2419                                  * and set it on OST objects. This happens when
2420                                  * migrating or extending an encrypted file
2421                                  * without the key.
2422                                  */
2423                                 if (filename_is_volatile(dentry->d_name.name,
2424                                                          dentry->d_name.len,
2425                                                          NULL) &&
2426                                     llcrypt_require_key(inode) == -ENOKEY) {
2427                                         struct file *ref_file;
2428                                         struct inode *ref_inode;
2429                                         struct ll_inode_info *ref_lli;
2430                                         struct cl_object *ref_obj;
2431                                         struct cl_attr ref_attr = { 0 };
2432                                         struct lu_env *env;
2433                                         __u16 refcheck;
2434
2435                                         rc = volatile_ref_file(
2436                                                 dentry->d_name.name,
2437                                                 dentry->d_name.len,
2438                                                 &ref_file);
2439                                         if (rc)
2440                                                 GOTO(out, rc);
2441
2442                                         ref_inode = file_inode(ref_file);
2443                                         if (!ref_inode) {
2444                                                 fput(ref_file);
2445                                                 GOTO(out, rc = -EINVAL);
2446                                         }
2447
2448                                         env = cl_env_get(&refcheck);
2449                                         if (IS_ERR(env))
2450                                                 GOTO(out, rc = PTR_ERR(env));
2451
2452                                         ref_lli = ll_i2info(ref_inode);
2453                                         ref_obj = ref_lli->lli_clob;
2454                                         cl_object_attr_lock(ref_obj);
2455                                         rc = cl_object_attr_get(env, ref_obj,
2456                                                                 &ref_attr);
2457                                         cl_object_attr_unlock(ref_obj);
2458                                         cl_env_put(env, &refcheck);
2459                                         fput(ref_file);
2460                                         if (rc)
2461                                                 GOTO(out, rc);
2462
2463                                         attr->ia_valid |= ATTR_SIZE;
2464                                         attr->ia_size = ref_attr.cat_size;
2465                                 }
2466                         }
2467                         rc = cl_setattr_ost(lli->lli_clob, attr, xvalid, flags);
2468                 }
2469         }
2470
2471         /* If the file was restored, it needs to set dirty flag.
2472          *
2473          * We've already sent MDS_DATA_MODIFIED flag in
2474          * ll_md_setattr() for truncate. However, the MDT refuses to
2475          * set the HS_DIRTY flag on released files, so we have to set
2476          * it again if the file has been restored. Please check how
2477          * LLIF_DATA_MODIFIED is set in vvp_io_setattr_fini().
2478          *
2479          * Please notice that if the file is not released, the previous
2480          * MDS_DATA_MODIFIED has taken effect and usually
2481          * LLIF_DATA_MODIFIED is not set(see vvp_io_setattr_fini()).
2482          * This way we can save an RPC for common open + trunc
2483          * operation. */
2484         if (test_and_clear_bit(LLIF_DATA_MODIFIED, &lli->lli_flags)) {
2485                 struct hsm_state_set hss = {
2486                         .hss_valid = HSS_SETMASK,
2487                         .hss_setmask = HS_DIRTY,
2488                 };
2489                 int rc2;
2490
2491                 rc2 = ll_hsm_state_set(inode, &hss);
2492                 /* truncate and write can happen at the same time, so that
2493                  * the file can be set modified even though the file is not
2494                  * restored from released state, and ll_hsm_state_set() is
2495                  * not applicable for the file, and rc2 < 0 is normal in this
2496                  * case. */
2497                 if (rc2 < 0)
2498                         CDEBUG(D_INFO, DFID "HSM set dirty failed: rc2 = %d\n",
2499                                PFID(ll_inode2fid(inode)), rc2);
2500         }
2501
2502         EXIT;
2503 out:
2504         if (op_data != NULL)
2505                 ll_finish_md_op_data(op_data);
2506
2507         if (S_ISREG(inode->i_mode)) {
2508                 ll_inode_lock(inode);
2509                 if ((attr->ia_valid & ATTR_SIZE) && !hsm_import)
2510                         inode_dio_wait(inode);
2511                 /* Once we've got the i_mutex, it's safe to set the S_NOSEC
2512                  * flag.  ll_update_inode (called from ll_md_setattr), clears
2513                  * inode flags, so there is a gap where S_NOSEC is not set.
2514                  * This can cause a writer to take the i_mutex unnecessarily,
2515                  * but this is safe to do and should be rare. */
2516                 inode_has_no_xattr(inode);
2517         }
2518
2519         if (!rc)
2520                 ll_stats_ops_tally(ll_i2sbi(inode), attr->ia_valid & ATTR_SIZE ?
2521                                         LPROC_LL_TRUNC : LPROC_LL_SETATTR,
2522                                    ktime_us_delta(ktime_get(), kstart));
2523 clear:
2524         CDEBUG(D_VFSTRACE|D_IOTRACE,
2525                "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",
2526                dentry->d_name.name, PFID(ll_inode2fid(inode)), inode,
2527                i_size_read(inode), attr->ia_valid, attr->ia_mode,
2528                attr->ia_uid.val, attr->ia_gid.val, attr->ia_size,
2529                (long long) attr->ia_atime.tv_sec, attr->ia_atime.tv_nsec,
2530                (long long) attr->ia_mtime.tv_sec, attr->ia_mtime.tv_nsec,
2531                (long long) attr->ia_ctime.tv_sec, attr->ia_ctime.tv_nsec, rc);
2532
2533         ll_clear_inode_lock_owner(inode);
2534
2535         RETURN(rc);
2536 }
2537
2538 int ll_setattr(struct mnt_idmap *map, struct dentry *de, struct iattr *attr)
2539 {
2540         int mode = de->d_inode->i_mode;
2541         enum op_xvalid xvalid = 0;
2542         int rc;
2543
2544         rc = setattr_prepare(map, de, attr);
2545         if (rc)
2546                 return rc;
2547
2548         rc = llcrypt_prepare_setattr(de, attr);
2549         if (rc)
2550                 return rc;
2551
2552         if ((attr->ia_valid & (ATTR_CTIME|ATTR_SIZE|ATTR_MODE)) ==
2553                               (ATTR_CTIME|ATTR_SIZE|ATTR_MODE))
2554                 xvalid |= OP_XVALID_OWNEROVERRIDE;
2555
2556         if (((attr->ia_valid & (ATTR_MODE|ATTR_FORCE|ATTR_SIZE)) ==
2557                                (ATTR_SIZE|ATTR_MODE)) &&
2558             (((mode & S_ISUID) && !(attr->ia_mode & S_ISUID)) ||
2559              (((mode & (S_ISGID|S_IXGRP)) == (S_ISGID|S_IXGRP)) &&
2560               !(attr->ia_mode & S_ISGID))))
2561                 attr->ia_valid |= ATTR_FORCE;
2562
2563         if ((attr->ia_valid & ATTR_MODE) &&
2564             (mode & S_ISUID) &&
2565             !(attr->ia_mode & S_ISUID) &&
2566             !(attr->ia_valid & ATTR_KILL_SUID))
2567                 attr->ia_valid |= ATTR_KILL_SUID;
2568
2569         if ((attr->ia_valid & ATTR_MODE) &&
2570             ((mode & (S_ISGID|S_IXGRP)) == (S_ISGID|S_IXGRP)) &&
2571             !(attr->ia_mode & S_ISGID) &&
2572             !(attr->ia_valid & ATTR_KILL_SGID))
2573                 attr->ia_valid |= ATTR_KILL_SGID;
2574
2575         return ll_setattr_raw(de, attr, xvalid, false);
2576 }
2577
2578 int ll_statfs_internal(struct ll_sb_info *sbi, struct obd_statfs *osfs,
2579                        u32 flags)
2580 {
2581         struct obd_statfs obd_osfs = { 0 };
2582         time64_t max_age;
2583         int rc;
2584
2585         ENTRY;
2586         max_age = ktime_get_seconds() - sbi->ll_statfs_max_age;
2587
2588         if (test_bit(LL_SBI_LAZYSTATFS, sbi->ll_flags))
2589                 flags |= OBD_STATFS_NODELAY;
2590
2591         rc = obd_statfs(NULL, sbi->ll_md_exp, osfs, max_age, flags);
2592         if (rc)
2593                 RETURN(rc);
2594
2595         osfs->os_type = LL_SUPER_MAGIC;
2596
2597         CDEBUG(D_SUPER, "MDC blocks %llu/%llu objects %llu/%llu\n",
2598               osfs->os_bavail, osfs->os_blocks, osfs->os_ffree, osfs->os_files);
2599
2600         if (osfs->os_state & OS_STATFS_SUM)
2601                 GOTO(out, rc);
2602
2603         rc = obd_statfs(NULL, sbi->ll_dt_exp, &obd_osfs, max_age, flags);
2604         if (rc) /* Possibly a filesystem with no OSTs.  Report MDT totals. */
2605                 GOTO(out, rc = 0);
2606
2607         CDEBUG(D_SUPER, "OSC blocks %llu/%llu objects %llu/%llu\n",
2608                obd_osfs.os_bavail, obd_osfs.os_blocks, obd_osfs.os_ffree,
2609                obd_osfs.os_files);
2610
2611         osfs->os_bsize = obd_osfs.os_bsize;
2612         osfs->os_blocks = obd_osfs.os_blocks;
2613         osfs->os_bfree = obd_osfs.os_bfree;
2614         osfs->os_bavail = obd_osfs.os_bavail;
2615
2616         /* If we have _some_ OSTs, but don't have as many free objects on the
2617          * OSTs as inodes on the MDTs, reduce the reported number of inodes
2618          * to compensate, so that the "inodes in use" number is correct.
2619          * This should be kept in sync with lod_statfs() behaviour.
2620          */
2621         if (obd_osfs.os_files && obd_osfs.os_ffree < osfs->os_ffree) {
2622                 osfs->os_files = (osfs->os_files - osfs->os_ffree) +
2623                                  obd_osfs.os_ffree;
2624                 osfs->os_ffree = obd_osfs.os_ffree;
2625         }
2626
2627 out:
2628         RETURN(rc);
2629 }
2630
2631 static int ll_statfs_project(struct inode *inode, struct kstatfs *sfs)
2632 {
2633         struct if_quotactl qctl = {
2634                 .qc_cmd = LUSTRE_Q_GETQUOTA,
2635                 .qc_type = PRJQUOTA,
2636                 .qc_valid = QC_GENERAL,
2637         };
2638         u64 limit, curblock;
2639         int ret;
2640
2641         qctl.qc_id = ll_i2info(inode)->lli_projid;
2642         ret = quotactl_ioctl(inode->i_sb, &qctl);
2643         if (ret) {
2644                 /* ignore errors if project ID does not have
2645                  * a quota limit or feature unsupported.
2646                  */
2647                 if (ret == -ESRCH || ret == -EOPNOTSUPP)
2648                         ret = 0;
2649                 return ret;
2650         }
2651
2652         limit = ((qctl.qc_dqblk.dqb_bsoftlimit ?
2653                  qctl.qc_dqblk.dqb_bsoftlimit :
2654                  qctl.qc_dqblk.dqb_bhardlimit) * 1024) / sfs->f_bsize;
2655         if (limit && sfs->f_blocks > limit) {
2656                 curblock = (qctl.qc_dqblk.dqb_curspace +
2657                                 sfs->f_bsize - 1) / sfs->f_bsize;
2658                 sfs->f_blocks = limit;
2659                 sfs->f_bfree = sfs->f_bavail =
2660                         (sfs->f_blocks > curblock) ?
2661                         (sfs->f_blocks - curblock) : 0;
2662         }
2663
2664         limit = qctl.qc_dqblk.dqb_isoftlimit ?
2665                 qctl.qc_dqblk.dqb_isoftlimit :
2666                 qctl.qc_dqblk.dqb_ihardlimit;
2667         if (limit && sfs->f_files > limit) {
2668                 sfs->f_files = limit;
2669                 sfs->f_ffree = (sfs->f_files >
2670                         qctl.qc_dqblk.dqb_curinodes) ?
2671                         (sfs->f_files - qctl.qc_dqblk.dqb_curinodes) : 0;
2672         }
2673
2674         return 0;
2675 }
2676
2677 int ll_statfs(struct dentry *de, struct kstatfs *sfs)
2678 {
2679         struct super_block *sb = de->d_sb;
2680         struct ll_sb_info *sbi = ll_s2sbi(sb);
2681         struct obd_statfs osfs;
2682         __u64 fsid = huge_encode_dev(sb->s_dev);
2683         ktime_t kstart = ktime_get();
2684         int rc;
2685
2686         CDEBUG(D_VFSTRACE, "VFS Op:sb=%s (%p)\n", sb->s_id, sb);
2687
2688         /* Some amount of caching on the client is allowed */
2689         rc = ll_statfs_internal(sbi, &osfs, OBD_STATFS_SUM);
2690         if (rc)
2691                 return rc;
2692
2693         statfs_unpack(sfs, &osfs);
2694
2695         /* We need to downshift for all 32-bit kernels, because we can't
2696          * tell if the kernel is being called via sys_statfs64() or not.
2697          * Stop before overflowing f_bsize - in which case it is better
2698          * to just risk EOVERFLOW if caller is using old sys_statfs(). */
2699         if (sizeof(long) < 8) {
2700                 while (osfs.os_blocks > ~0UL && sfs->f_bsize < 0x40000000) {
2701                         sfs->f_bsize <<= 1;
2702
2703                         osfs.os_blocks >>= 1;
2704                         osfs.os_bfree >>= 1;
2705                         osfs.os_bavail >>= 1;
2706                 }
2707         }
2708
2709         sfs->f_blocks = osfs.os_blocks;
2710         sfs->f_bfree = osfs.os_bfree;
2711         sfs->f_bavail = osfs.os_bavail;
2712         sfs->f_fsid.val[0] = (__u32)fsid;
2713         sfs->f_fsid.val[1] = (__u32)(fsid >> 32);
2714         if (ll_i2info(de->d_inode)->lli_projid &&
2715                 test_bit(LL_SBI_STATFS_PROJECT, sbi->ll_flags) &&
2716             test_bit(LLIF_PROJECT_INHERIT, &ll_i2info(de->d_inode)->lli_flags))
2717                 return ll_statfs_project(de->d_inode, sfs);
2718
2719         ll_stats_ops_tally(sbi, LPROC_LL_STATFS,
2720                            ktime_us_delta(ktime_get(), kstart));
2721
2722         return 0;
2723 }
2724
2725 void ll_inode_size_lock(struct inode *inode)
2726 {
2727         struct ll_inode_info *lli;
2728
2729         LASSERT(!S_ISDIR(inode->i_mode));
2730
2731         lli = ll_i2info(inode);
2732         mutex_lock(&lli->lli_size_mutex);
2733         lli->lli_size_lock_owner = current;
2734 }
2735
2736 void ll_inode_size_unlock(struct inode *inode)
2737 {
2738         struct ll_inode_info *lli;
2739
2740         lli = ll_i2info(inode);
2741         lli->lli_size_lock_owner = NULL;
2742         mutex_unlock(&lli->lli_size_mutex);
2743 }
2744
2745 int ll_inode_size_trylock(struct inode *inode)
2746 {
2747         struct ll_inode_info *lli;
2748
2749         LASSERT(!S_ISDIR(inode->i_mode));
2750
2751         lli = ll_i2info(inode);
2752         return mutex_trylock(&lli->lli_size_mutex);
2753 }
2754
2755 void ll_update_inode_flags(struct inode *inode, unsigned int ext_flags)
2756 {
2757         /* do not clear encryption flag */
2758         ext_flags |= ll_inode_to_ext_flags(inode->i_flags) & LUSTRE_ENCRYPT_FL;
2759         inode->i_flags = ll_ext_to_inode_flags(ext_flags);
2760         if (ext_flags & LUSTRE_PROJINHERIT_FL)
2761                 set_bit(LLIF_PROJECT_INHERIT, &ll_i2info(inode)->lli_flags);
2762         else
2763                 clear_bit(LLIF_PROJECT_INHERIT, &ll_i2info(inode)->lli_flags);
2764 }
2765
2766 int ll_update_inode(struct inode *inode, struct lustre_md *md)
2767 {
2768         struct ll_inode_info *lli = ll_i2info(inode);
2769         struct mdt_body *body = md->body;
2770         struct ll_sb_info *sbi = ll_i2sbi(inode);
2771         bool api32;
2772         int rc = 0;
2773
2774         if (body->mbo_valid & OBD_MD_FLEASIZE) {
2775                 rc = cl_file_inode_init(inode, md);
2776                 if (rc)
2777                         return rc;
2778         }
2779
2780         if (S_ISDIR(inode->i_mode)) {
2781                 rc = ll_update_lsm_md(inode, md);
2782                 if (rc != 0)
2783                         return rc;
2784         }
2785
2786         if (body->mbo_valid & OBD_MD_FLACL)
2787                 lli_replace_acl(lli, md);
2788
2789         api32 = test_bit(LL_SBI_32BIT_API, sbi->ll_flags);
2790         inode->i_ino = cl_fid_build_ino(&body->mbo_fid1, api32);
2791         inode->i_generation = cl_fid_build_gen(&body->mbo_fid1);
2792
2793         if (body->mbo_valid & OBD_MD_FLATIME) {
2794                 if (body->mbo_atime > inode_get_atime_sec(inode))
2795                         inode_set_atime(inode, body->mbo_atime, 0);
2796                 lli->lli_atime = body->mbo_atime;
2797         }
2798
2799         if (body->mbo_valid & OBD_MD_FLMTIME) {
2800                 if (body->mbo_mtime > inode_get_mtime_sec(inode)) {
2801                         CDEBUG(D_INODE,
2802                                "setting ino %lu mtime from %lld to %llu\n",
2803                                inode->i_ino, (s64) inode_get_mtime_sec(inode),
2804                                body->mbo_mtime);
2805                         inode_set_mtime(inode, body->mbo_mtime, 0);
2806                 }
2807                 lli->lli_mtime = body->mbo_mtime;
2808         }
2809
2810         if (body->mbo_valid & OBD_MD_FLCTIME) {
2811                 if (body->mbo_ctime > inode_get_ctime_sec(inode))
2812                         inode_set_ctime(inode, body->mbo_ctime, 0);
2813                 lli->lli_ctime = body->mbo_ctime;
2814         }
2815
2816         if (body->mbo_valid & OBD_MD_FLBTIME)
2817                 lli->lli_btime = body->mbo_btime;
2818
2819         /* Clear i_flags to remove S_NOSEC before permissions are updated */
2820         if (body->mbo_valid & OBD_MD_FLFLAGS)
2821                 ll_update_inode_flags(inode, body->mbo_flags);
2822         if (body->mbo_valid & OBD_MD_FLMODE)
2823                 inode->i_mode = (inode->i_mode & S_IFMT) |
2824                                 (body->mbo_mode & ~S_IFMT);
2825
2826         if (body->mbo_valid & OBD_MD_FLTYPE)
2827                 inode->i_mode = (inode->i_mode & ~S_IFMT) |
2828                                 (body->mbo_mode & S_IFMT);
2829
2830         LASSERT(inode->i_mode != 0);
2831         if (body->mbo_valid & OBD_MD_FLUID)
2832                 inode->i_uid = make_kuid(&init_user_ns, body->mbo_uid);
2833         if (body->mbo_valid & OBD_MD_FLGID)
2834                 inode->i_gid = make_kgid(&init_user_ns, body->mbo_gid);
2835         if (body->mbo_valid & OBD_MD_FLPROJID)
2836                 lli->lli_projid = body->mbo_projid;
2837         if (body->mbo_valid & OBD_MD_FLNLINK) {
2838                 spin_lock(&inode->i_lock);
2839                 set_nlink(inode, body->mbo_nlink);
2840                 spin_unlock(&inode->i_lock);
2841         }
2842         if (body->mbo_valid & OBD_MD_FLRDEV)
2843                 inode->i_rdev = old_decode_dev(body->mbo_rdev);
2844
2845         if (body->mbo_valid & OBD_MD_FLID) {
2846                 /* FID shouldn't be changed! */
2847                 if (fid_is_sane(&lli->lli_fid)) {
2848                         LASSERTF(lu_fid_eq(&lli->lli_fid, &body->mbo_fid1),
2849                                  "Trying to change FID "DFID" to the "DFID", inode "DFID"(%px)\n",
2850                                  PFID(&lli->lli_fid), PFID(&body->mbo_fid1),
2851                                  PFID(ll_inode2fid(inode)), inode);
2852                 } else {
2853                         lli->lli_fid = body->mbo_fid1;
2854                 }
2855         }
2856
2857         LASSERT(fid_seq(&lli->lli_fid) != 0);
2858
2859         /* In case of encrypted file without the key, please do not lose
2860          * clear text size stored into lli_lazysize in ll_merge_attr(),
2861          * we will need it in ll_prepare_close().
2862          */
2863         if (lli->lli_attr_valid & OBD_MD_FLLAZYSIZE && lli->lli_lazysize &&
2864             llcrypt_require_key(inode) == -ENOKEY)
2865                 lli->lli_attr_valid = body->mbo_valid | OBD_MD_FLLAZYSIZE;
2866         else
2867                 lli->lli_attr_valid = body->mbo_valid;
2868         if (body->mbo_valid & OBD_MD_FLSIZE) {
2869                 i_size_write(inode, body->mbo_size);
2870
2871                 CDEBUG(D_VFSTRACE, "inode="DFID", updating i_size %llu\n",
2872                        PFID(ll_inode2fid(inode)),
2873                        (unsigned long long)body->mbo_size);
2874
2875                 if (body->mbo_valid & OBD_MD_FLBLOCKS)
2876                         inode->i_blocks = body->mbo_blocks;
2877         } else {
2878                 if (body->mbo_valid & OBD_MD_FLLAZYSIZE)
2879                         lli->lli_lazysize = body->mbo_size;
2880                 if (body->mbo_valid & OBD_MD_FLLAZYBLOCKS)
2881                         lli->lli_lazyblocks = body->mbo_blocks;
2882         }
2883
2884         if (body->mbo_valid & OBD_MD_TSTATE) {
2885                 /* Set LLIF_FILE_RESTORING if restore ongoing and
2886                  * clear it when done to ensure to start again
2887                  * glimpsing updated attrs
2888                  */
2889                 if (body->mbo_t_state & MS_RESTORE)
2890                         set_bit(LLIF_FILE_RESTORING, &lli->lli_flags);
2891                 else
2892                         clear_bit(LLIF_FILE_RESTORING, &lli->lli_flags);
2893         }
2894
2895         return 0;
2896 }
2897
2898 /* child default LMV is inherited from parent */
2899 static inline bool ll_default_lmv_inherited(struct lmv_stripe_md *pdmv,
2900                                             struct lmv_stripe_md *cdmv)
2901 {
2902         if (!pdmv || !cdmv)
2903                 return false;
2904
2905         if (pdmv->lsm_md_magic != cdmv->lsm_md_magic ||
2906             pdmv->lsm_md_stripe_count != cdmv->lsm_md_stripe_count ||
2907             pdmv->lsm_md_master_mdt_index != cdmv->lsm_md_master_mdt_index ||
2908             pdmv->lsm_md_hash_type != cdmv->lsm_md_hash_type)
2909                 return false;
2910
2911         if (cdmv->lsm_md_max_inherit !=
2912             lmv_inherit_next(pdmv->lsm_md_max_inherit))
2913                 return false;
2914
2915         if (cdmv->lsm_md_max_inherit_rr !=
2916             lmv_inherit_rr_next(pdmv->lsm_md_max_inherit_rr))
2917                 return false;
2918
2919         return true;
2920 }
2921
2922 /* if default LMV is implicitly inherited, subdir default LMV is maintained on
2923  * client side.
2924  */
2925 static int ll_dir_default_lmv_inherit(struct inode *dir, struct inode *inode)
2926 {
2927         struct ll_inode_info *plli = ll_i2info(dir);
2928         struct ll_inode_info *lli = ll_i2info(inode);
2929         struct lmv_stripe_object *plsm_obj;
2930         struct lmv_stripe_object *lsm_obj;
2931         int rc = 0;
2932
2933         ENTRY;
2934
2935         /* ROOT default LMV is not inherited */
2936         if (is_root_inode(dir) ||
2937             !(exp_connect_flags2(ll_i2mdexp(dir)) &
2938                                  OBD_CONNECT2_DMV_IMP_INHERIT))
2939                 RETURN(0);
2940
2941         /* nothing to do if no default LMV on both */
2942         if (!plli->lli_def_lsm_obj && !lli->lli_def_lsm_obj)
2943                 RETURN(0);
2944
2945         /* subdir default LMV comes from disk */
2946         if (lli->lli_def_lsm_obj && lli->lli_def_lsm_obj_set)
2947                 RETURN(0);
2948
2949         /* delete subdir default LMV if parent's is deleted or becomes
2950          * uninheritable.
2951          */
2952         down_read(&plli->lli_lsm_sem);
2953         plsm_obj = plli->lli_def_lsm_obj;
2954         if (!plsm_obj ||
2955             !lmv_is_inheritable(plsm_obj->lso_lsm.lsm_md_max_inherit)) {
2956                 if (lli->lli_def_lsm_obj && !lli->lli_def_lsm_obj_set) {
2957                         down_write(&lli->lli_lsm_sem);
2958                         if (!lli->lli_def_lsm_obj_set) {
2959                                 lmv_stripe_object_put(&lli->lli_def_lsm_obj);
2960                                 lli->lli_inherit_depth = 0;
2961                         }
2962                         up_write(&lli->lli_lsm_sem);
2963                 }
2964                 GOTO(unlock_parent, rc = 0);
2965         }
2966
2967         /* do nothing if inherited LMV is unchanged */
2968         if (lli->lli_def_lsm_obj) {
2969                 rc = 1;
2970                 down_read(&lli->lli_lsm_sem);
2971                 if (!lli->lli_def_lsm_obj_set)
2972                         rc = lmv_object_inherited(plsm_obj,
2973                                                   lli->lli_def_lsm_obj);
2974                 up_read(&lli->lli_lsm_sem);
2975                 if (rc == 1)
2976                         GOTO(unlock_parent, rc = 0);
2977         }
2978
2979         /* inherit default LMV */
2980         down_write(&lli->lli_lsm_sem);
2981         if (lli->lli_def_lsm_obj) {
2982                 /* checked above, but in case of race, check again with lock */
2983                 if (lli->lli_def_lsm_obj_set)
2984                         GOTO(unlock_child, rc = 0);
2985                 /* always update subdir default LMV in case parent's changed */
2986                 lsm_obj = lli->lli_def_lsm_obj;
2987         } else {
2988                 lsm_obj = lmv_stripe_object_alloc(LMV_USER_MAGIC, NULL, 0);
2989                 if (IS_ERR(lsm_obj))
2990                         GOTO(unlock_child, rc = PTR_ERR(lsm_obj));
2991                 lli->lli_def_lsm_obj = lsm_obj;
2992         }
2993
2994         lsm_obj->lso_lsm = plsm_obj->lso_lsm;
2995         lsm_obj->lso_lsm.lsm_md_max_inherit =
2996                 lmv_inherit_next(plsm_obj->lso_lsm.lsm_md_max_inherit);
2997         lsm_obj->lso_lsm.lsm_md_max_inherit_rr =
2998                 lmv_inherit_rr_next(plsm_obj->lso_lsm.lsm_md_max_inherit_rr);
2999         lli->lli_inherit_depth = plli->lli_inherit_depth + 1;
3000
3001         lmv_stripe_object_dump(D_INODE, lsm_obj);
3002
3003         EXIT;
3004 unlock_child:
3005         up_write(&lli->lli_lsm_sem);
3006 unlock_parent:
3007         up_read(&plli->lli_lsm_sem);
3008
3009         return rc;
3010 }
3011
3012 enum lsm_sem_class {
3013         LSM_SEM_PARENT,
3014         LSM_SEM_CHILD,
3015 };
3016
3017 /**
3018  * Update directory depth and default LMV
3019  *
3020  * Update directory depth to ROOT and inherit default LMV from parent if
3021  * parent's default LMV is inheritable. The default LMV set with command
3022  * "lfs setdirstripe -D ..." is stored on MDT, while the inherited default LMV
3023  * is generated at runtime on client side.
3024  *
3025  * \param[in]   dir     parent directory inode
3026  * \param[in]   de      dentry
3027  */
3028 void ll_update_dir_depth_dmv(struct inode *dir, struct dentry *de)
3029 {
3030         struct inode *inode = de->d_inode;
3031         struct ll_inode_info *plli;
3032         struct ll_inode_info *lli;
3033
3034         LASSERT(S_ISDIR(inode->i_mode));
3035         if (inode == dir)
3036                 return;
3037
3038         plli = ll_i2info(dir);
3039         lli = ll_i2info(inode);
3040         lli->lli_dir_depth = plli->lli_dir_depth + 1;
3041         if (lli->lli_def_lsm_obj && lli->lli_def_lsm_obj_set) {
3042                 if (plli->lli_def_lsm_obj) {
3043                         down_read_nested(&plli->lli_lsm_sem, LSM_SEM_PARENT);
3044                         down_read_nested(&lli->lli_lsm_sem, LSM_SEM_CHILD);
3045                         if (lmv_object_inherited(plli->lli_def_lsm_obj,
3046                                                  lli->lli_def_lsm_obj))
3047                                 lli->lli_inherit_depth =
3048                                         plli->lli_inherit_depth + 1;
3049                         else
3050                                 /* in case parent default LMV changed */
3051                                 lli->lli_inherit_depth = 0;
3052                         up_read(&lli->lli_lsm_sem);
3053                         up_read(&plli->lli_lsm_sem);
3054                 } else {
3055                         /* in case parent default LMV deleted */
3056                         lli->lli_inherit_depth = 0;
3057                 }
3058         } else {
3059                 ll_dir_default_lmv_inherit(dir, inode);
3060         }
3061
3062         if (lli->lli_def_lsm_obj)
3063                 CDEBUG(D_INODE,
3064                        "%s "DFID" depth %hu %s default LMV inherit depth %hu\n",
3065                        de->d_name.name, PFID(&lli->lli_fid), lli->lli_dir_depth,
3066                        lli->lli_def_lsm_obj_set ? "server" : "client",
3067                        lli->lli_inherit_depth);
3068 }
3069
3070 void ll_truncate_inode_pages_final(struct inode *inode)
3071 {
3072         struct address_space *mapping = &inode->i_data;
3073         unsigned long nrpages;
3074         unsigned long flags;
3075
3076         LASSERTF((inode->i_state & I_FREEING) || inode_is_locked(inode),
3077                  DFID ":inode %px state %#lx, lli_flags %#lx\n",
3078                  PFID(ll_inode2fid(inode)), inode, inode->i_state,
3079                  ll_i2info(inode)->lli_flags);
3080
3081         truncate_inode_pages_final(mapping);
3082
3083         CFS_FAIL_TIMEOUT(OBD_FAIL_LLITE_DELAY_TRUNCATE, 5);
3084
3085         /* Workaround for LU-118: Note nrpages may not be totally updated when
3086          * truncate_inode_pages() returns, as there can be a page in the process
3087          * of deletion (inside __delete_from_page_cache()) in the specified
3088          * range. Thus mapping->nrpages can be non-zero when this function
3089          * returns even after truncation of the whole mapping.  Only do this if
3090          * npages isn't already zero.
3091          */
3092         nrpages = mapping->nrpages;
3093         if (nrpages) {
3094                 ll_xa_lock_irqsave(&mapping->i_pages, flags);
3095                 nrpages = mapping->nrpages;
3096                 ll_xa_unlock_irqrestore(&mapping->i_pages, flags);
3097         } /* Workaround end */
3098
3099         if (nrpages) {
3100 #ifdef HAVE_XARRAY_SUPPORT
3101                 XA_STATE(xas, &mapping->i_pages, 0);
3102                 struct page *page;
3103 #endif
3104                 CWARN("%s: inode="DFID"(%p) nrpages=%lu "
3105                          "state %#lx, lli_flags %#lx, "
3106                          "see https://jira.whamcloud.com/browse/LU-118\n",
3107                          ll_i2sbi(inode)->ll_fsname,
3108                          PFID(ll_inode2fid(inode)), inode, nrpages,
3109                          inode->i_state, ll_i2info(inode)->lli_flags);
3110 #ifdef HAVE_XARRAY_SUPPORT
3111                 rcu_read_lock();
3112                 xas_for_each(&xas, page, ULONG_MAX) {
3113                         if (xas_retry(&xas, page))
3114                                 continue;
3115
3116                         if (xa_is_value(page))
3117                                 continue;
3118
3119                         /*
3120                          * We can only have non-uptodate pages
3121                          * without internal state at this point
3122                          */
3123                         LASSERTF(!PageUptodate(page) &&
3124                                  !PageDirty(page) &&
3125                                  !PagePrivate(page),
3126                                  "%px", page);
3127                 }
3128                 rcu_read_unlock();
3129 #endif
3130         }
3131 }
3132
3133 int ll_read_inode2(struct inode *inode, void *opaque)
3134 {
3135         struct lustre_md *md = opaque;
3136         struct ll_inode_info *lli = ll_i2info(inode);
3137         int     rc;
3138         ENTRY;
3139
3140         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
3141                PFID(&lli->lli_fid), inode);
3142
3143         /* Core attributes from the MDS first.  This is a new inode, and
3144          * the VFS doesn't zero times in the core inode so we have to do
3145          * it ourselves.  They will be overwritten by either MDS or OST
3146          * attributes - we just need to make sure they aren't newer.
3147          */
3148         inode_set_mtime(inode, 0, 0);
3149         inode_set_atime(inode, 0, 0);
3150         inode_set_ctime(inode, 0, 0);
3151         inode->i_rdev = 0;
3152         rc = ll_update_inode(inode, md);
3153         if (rc != 0)
3154                 RETURN(rc);
3155
3156         /* OIDEBUG(inode); */
3157
3158 #ifdef HAVE_BACKING_DEV_INFO
3159         /* initializing backing dev info. */
3160         inode->i_mapping->backing_dev_info = &s2lsi(inode->i_sb)->lsi_bdi;
3161 #endif
3162         if (S_ISREG(inode->i_mode)) {
3163                 struct ll_sb_info *sbi = ll_i2sbi(inode);
3164                 inode->i_op = &ll_file_inode_operations;
3165                 inode->i_fop = sbi->ll_fop;
3166                 inode->i_mapping->a_ops = (struct address_space_operations *)&ll_aops;
3167                 EXIT;
3168         } else if (S_ISDIR(inode->i_mode)) {
3169                 inode->i_op = &ll_dir_inode_operations;
3170                 inode->i_fop = &ll_dir_operations;
3171                 EXIT;
3172         } else if (S_ISLNK(inode->i_mode)) {
3173                 inode->i_op = &ll_fast_symlink_inode_operations;
3174                 EXIT;
3175         } else {
3176                 inode->i_op = &ll_special_inode_operations;
3177
3178                 init_special_inode(inode, inode->i_mode,
3179                                    inode->i_rdev);
3180
3181                 EXIT;
3182         }
3183
3184         return 0;
3185 }
3186
3187 void ll_delete_inode(struct inode *inode)
3188 {
3189         struct ll_inode_info *lli = ll_i2info(inode);
3190         ENTRY;
3191
3192         if (S_ISREG(inode->i_mode) && lli->lli_clob != NULL) {
3193                 /* It is last chance to write out dirty pages,
3194                  * otherwise we may lose data while umount.
3195                  *
3196                  * If i_nlink is 0 then just discard data. This is safe because
3197                  * local inode gets i_nlink 0 from server only for the last
3198                  * unlink, so that file is not opened somewhere else
3199                  */
3200                 cl_sync_file_range(inode, 0, OBD_OBJECT_EOF, inode->i_nlink ?
3201                                    CL_FSYNC_LOCAL : CL_FSYNC_DISCARD, 1);
3202         }
3203
3204         ll_truncate_inode_pages_final(inode);
3205         ll_clear_inode(inode);
3206         clear_inode(inode);
3207
3208         EXIT;
3209 }
3210
3211 static int fileattr_get(struct inode *inode, int *flags,
3212                         u32 *xflags,
3213                         u32 *projid)
3214 {
3215         struct ll_sb_info *sbi = ll_i2sbi(inode);
3216         struct ptlrpc_request *req = NULL;
3217         struct md_op_data *op_data;
3218         struct mdt_body *body;
3219         int rc;
3220
3221         ENTRY;
3222         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL,
3223                                      0, 0, LUSTRE_OPC_ANY,
3224                                      NULL);
3225         if (IS_ERR(op_data))
3226                 RETURN(PTR_ERR(op_data));
3227
3228         op_data->op_valid = OBD_MD_FLFLAGS;
3229         rc = md_getattr(sbi->ll_md_exp, op_data, &req);
3230         ll_finish_md_op_data(op_data);
3231         if (rc) {
3232                 CERROR("%s: failure inode "DFID": rc = %d\n",
3233                        sbi->ll_md_exp->exp_obd->obd_name,
3234                        PFID(ll_inode2fid(inode)), rc);
3235                 RETURN(-abs(rc));
3236         }
3237
3238         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
3239
3240         *flags = body->mbo_flags;
3241         /* if Lustre specific LUSTRE_ENCRYPT_FL flag is set, also set
3242          * ext4 equivalent to please lsattr and other e2fsprogs tools
3243          */
3244         if (*flags & LUSTRE_ENCRYPT_FL)
3245                 *flags |= STATX_ATTR_ENCRYPTED;
3246
3247         ptlrpc_req_finished(req);
3248
3249         *xflags = ll_inode_flags_to_xflags(inode->i_flags);
3250         if (test_bit(LLIF_PROJECT_INHERIT, &ll_i2info(inode)->lli_flags))
3251                 *xflags |= FS_XFLAG_PROJINHERIT;
3252         *projid = ll_i2info(inode)->lli_projid;
3253
3254         RETURN(0);
3255 }
3256
3257 static int fileattr_set(struct inode *inode, int flags)
3258 {
3259         struct ll_sb_info *sbi = ll_i2sbi(inode);
3260         struct ptlrpc_request *req = NULL;
3261         struct md_op_data *op_data;
3262         struct cl_object *obj;
3263         struct fsxattr fa = { 0 };
3264         struct iattr *attr;
3265         int rc;
3266
3267         ENTRY;
3268         fa.fsx_projid = ll_i2info(inode)->lli_projid;
3269         if (flags & LUSTRE_PROJINHERIT_FL)
3270                 fa.fsx_xflags = FS_XFLAG_PROJINHERIT;
3271
3272         rc = ll_ioctl_check_project(inode, fa.fsx_xflags,
3273                                     fa.fsx_projid);
3274         if (rc)
3275                 RETURN(rc);
3276
3277         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
3278                                      LUSTRE_OPC_ANY, NULL);
3279         if (IS_ERR(op_data))
3280                 RETURN(PTR_ERR(op_data));
3281
3282         op_data->op_attr_flags = flags;
3283         op_data->op_xvalid |= OP_XVALID_FLAGS;
3284         rc = md_setattr(sbi->ll_md_exp, op_data, NULL, 0, &req);
3285         ll_finish_md_op_data(op_data);
3286         ptlrpc_req_finished(req);
3287         if (rc)
3288                 RETURN(rc);
3289
3290         ll_update_inode_flags(inode, flags);
3291
3292         obj = ll_i2info(inode)->lli_clob;
3293         if (obj == NULL)
3294                 RETURN(0);
3295
3296         OBD_ALLOC_PTR(attr);
3297         if (attr == NULL)
3298                 RETURN(-ENOMEM);
3299
3300         rc = cl_setattr_ost(obj, attr, OP_XVALID_FLAGS, flags);
3301
3302         OBD_FREE_PTR(attr);
3303         RETURN(rc);
3304 }
3305
3306 #ifdef HAVE_FILEATTR_GET
3307 int ll_fileattr_get(struct dentry *dentry, struct fileattr *fa)
3308 {
3309         return fileattr_get(d_inode(dentry), &fa->flags,
3310                             &fa->fsx_xflags, &fa->fsx_projid);
3311 }
3312
3313 int ll_fileattr_set(struct mnt_idmap *mnt_userns,
3314                     struct dentry *dentry, struct fileattr *fa)
3315 {
3316         if (fa->fsx_valid)
3317                 return ll_set_project(d_inode(dentry), fa->fsx_xflags,
3318                                       fa->fsx_projid);
3319         else
3320                 return fileattr_set(d_inode(dentry), fa->flags);
3321 }
3322 #endif /* HAVE_FILEATTR_GET */
3323
3324 /* ioctl commands shared between files and directories */
3325 int ll_iocontrol(struct inode *inode, struct file *file,
3326                  unsigned int cmd, void __user *uarg)
3327 {
3328         int rc;
3329         ENTRY;
3330
3331         switch (cmd) {
3332         case BLKSSZGET:
3333                 RETURN(put_user(PAGE_SIZE, (int __user *)uarg));
3334         case LL_IOC_GETVERSION:
3335         case FS_IOC_GETVERSION:
3336                 RETURN(put_user(inode->i_generation, (int __user *)uarg));
3337
3338 #ifndef HAVE_FILEATTR_GET
3339         case FS_IOC_GETFLAGS: {
3340                 u32 xflags = 0, projid = 0;
3341                 int flags = 0;
3342
3343                 if (!ll_access_ok(uarg, sizeof(int)))
3344                         RETURN(-EFAULT);
3345                 rc = fileattr_get(file->f_inode, &flags, &xflags, &projid);
3346                 if (rc)
3347                         RETURN(rc);
3348
3349                 RETURN(put_user(flags, (int __user *)uarg));
3350         }
3351         case FS_IOC_SETFLAGS: {
3352                 int flags = 0;
3353
3354                 if (get_user(flags, (int __user *)uarg))
3355                         RETURN(-EFAULT);
3356
3357                 RETURN(fileattr_set(file->f_inode, flags));
3358         }
3359         case FS_IOC_FSGETXATTR:
3360                 RETURN(ll_ioctl_fsgetxattr(inode, cmd, uarg));
3361         case FS_IOC_FSSETXATTR:
3362                 RETURN(ll_ioctl_fssetxattr(inode, cmd, uarg));
3363 #endif /* HAVE_FILEATTR_GET */
3364         case LL_IOC_PROJECT:
3365                 RETURN(ll_ioctl_project(file, cmd, uarg));
3366         case IOC_OBD_STATFS:
3367                 RETURN(ll_obd_statfs(inode, uarg));
3368         case LL_IOC_GET_MDTIDX: {
3369                 if (!ll_access_ok(uarg, sizeof(rc)))
3370                         RETURN(-EFAULT);
3371
3372                 rc = ll_get_mdt_idx(inode);
3373                 if (rc < 0)
3374                         RETURN(rc);
3375
3376                 if (put_user(rc, (int __user *)uarg))
3377                         RETURN(-EFAULT);
3378
3379                 RETURN(0);
3380         }
3381         case LL_IOC_FLUSHCTX:
3382                 RETURN(ll_flush_ctx(inode));
3383 #ifdef HAVE_LUSTRE_CRYPTO
3384         case LL_IOC_ADD_ENCRYPTION_KEY:
3385                 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
3386                         return -EOPNOTSUPP;
3387                 rc = llcrypt_ioctl_add_key(file, uarg);
3388 #ifdef CONFIG_LL_ENCRYPTION
3389                 if (!rc && S_ISDIR(inode->i_mode))
3390                         obd_pool_add_user();
3391 #endif
3392                 RETURN(rc);
3393         case LL_IOC_GET_ENCRYPTION_KEY_STATUS:
3394                 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
3395                         return -EOPNOTSUPP;
3396                 RETURN(llcrypt_ioctl_get_key_status(file, uarg));
3397         case LL_IOC_GET_ENCRYPTION_POLICY_EX:
3398                 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
3399                         return -EOPNOTSUPP;
3400                 RETURN(llcrypt_ioctl_get_policy_ex(file, uarg));
3401         case LL_IOC_SET_ENCRYPTION_POLICY:
3402                 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
3403                         return -EOPNOTSUPP;
3404                 RETURN(llcrypt_ioctl_set_policy(file, uarg));
3405         case LL_IOC_REMOVE_ENCRYPTION_KEY:
3406                 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
3407                         return -EOPNOTSUPP;
3408                 RETURN(llcrypt_ioctl_remove_key(file, uarg));
3409         case LL_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
3410                 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
3411                         return -EOPNOTSUPP;
3412                 RETURN(llcrypt_ioctl_remove_key_all_users(file, uarg));
3413 #endif
3414         case LL_IOC_GETPARENT:
3415                 RETURN(ll_getparent(file, uarg));
3416         case LL_IOC_PATH2FID:
3417                 if (copy_to_user(uarg, ll_inode2fid(inode),
3418                                  sizeof(struct lu_fid)))
3419                         RETURN(-EFAULT);
3420                 RETURN(0);
3421         case LL_IOC_UNLOCK_FOREIGN: {
3422                 struct dentry *dentry = file_dentry(file);
3423
3424                 /* if not a foreign symlink do nothing */
3425                 if (ll_foreign_is_removable(dentry, true)) {
3426                         CDEBUG(D_INFO,
3427                                "prevent unlink of non-foreign file ("DFID")\n",
3428                                PFID(ll_inode2fid(inode)));
3429                         RETURN(-EOPNOTSUPP);
3430                 }
3431                 RETURN(0);
3432         }
3433         case OBD_IOC_FID2PATH:
3434                 RETURN(ll_fid2path(inode, uarg));
3435 #ifdef OBD_IOC_GETNAME_OLD
3436         case_OBD_IOC_DEPRECATED_FT(OBD_IOC_GETNAME_OLD,
3437                                    ll_i2sbi(inode)->ll_md_exp->exp_obd->obd_name,
3438                                    2, 16);
3439 #endif
3440         case OBD_IOC_GETDTNAME:
3441         case OBD_IOC_GETMDNAME:
3442                 RETURN(ll_get_obd_name(inode, cmd, uarg));
3443         default:
3444                 RETURN(-ENOTTY);
3445         }
3446
3447         RETURN(0);
3448 }
3449
3450 int ll_flush_ctx(struct inode *inode)
3451 {
3452         struct ll_sb_info  *sbi = ll_i2sbi(inode);
3453
3454         CDEBUG(D_SEC, "flush context for user %d\n",
3455                from_kuid(&init_user_ns, current_uid()));
3456
3457         obd_set_info_async(NULL, sbi->ll_md_exp,
3458                            sizeof(KEY_FLUSH_CTX), KEY_FLUSH_CTX,
3459                            0, NULL, NULL);
3460         obd_set_info_async(NULL, sbi->ll_dt_exp,
3461                            sizeof(KEY_FLUSH_CTX), KEY_FLUSH_CTX,
3462                            0, NULL, NULL);
3463         return 0;
3464 }
3465
3466 /* umount -f client means force down, don't save state */
3467 void ll_umount_begin(struct super_block *sb)
3468 {
3469         struct ll_sb_info *sbi = ll_s2sbi(sb);
3470         struct obd_device *obd;
3471         struct obd_ioctl_data *ioc_data;
3472         int cnt;
3473         ENTRY;
3474
3475         CDEBUG(D_VFSTRACE, "VFS Op: superblock %p count %d active %d\n", sb,
3476                sb->s_count, atomic_read(&sb->s_active));
3477
3478         obd = class_exp2obd(sbi->ll_md_exp);
3479         if (obd == NULL) {
3480                 CERROR("Invalid MDC connection handle %#llx\n",
3481                        sbi->ll_md_exp->exp_handle.h_cookie);
3482                 EXIT;
3483                 return;
3484         }
3485         obd->obd_force = 1;
3486
3487         obd = class_exp2obd(sbi->ll_dt_exp);
3488         if (obd == NULL) {
3489                 CERROR("Invalid LOV connection handle %#llx\n",
3490                        sbi->ll_dt_exp->exp_handle.h_cookie);
3491                 EXIT;
3492                 return;
3493         }
3494         obd->obd_force = 1;
3495
3496         OBD_ALLOC_PTR(ioc_data);
3497         if (ioc_data) {
3498                 obd_iocontrol(OBD_IOC_SET_ACTIVE, sbi->ll_md_exp,
3499                               sizeof *ioc_data, ioc_data, NULL);
3500
3501                 obd_iocontrol(OBD_IOC_SET_ACTIVE, sbi->ll_dt_exp,
3502                               sizeof *ioc_data, ioc_data, NULL);
3503
3504                 OBD_FREE_PTR(ioc_data);
3505         }
3506
3507         /* Really, we'd like to wait until there are no requests outstanding,
3508          * and then continue.  For now, we just periodically checking for vfs
3509          * to decrement mnt_cnt and hope to finish it within 10sec.
3510          */
3511         cnt = 10;
3512         while (cnt > 0 &&
3513                !may_umount(sbi->ll_mnt.mnt)) {
3514                 ssleep(1);
3515                 cnt -= 1;
3516         }
3517
3518         EXIT;
3519 }
3520
3521 int ll_remount_fs(struct super_block *sb, int *flags, char *data)
3522 {
3523         struct ll_sb_info *sbi = ll_s2sbi(sb);
3524         char *profilenm = get_profile_name(sb);
3525         int err;
3526         __u32 read_only;
3527
3528         if ((*flags & MS_RDONLY) != (sb->s_flags & SB_RDONLY)) {
3529                 read_only = *flags & MS_RDONLY;
3530                 err = obd_set_info_async(NULL, sbi->ll_md_exp,
3531                                          sizeof(KEY_READ_ONLY),
3532                                          KEY_READ_ONLY, sizeof(read_only),
3533                                          &read_only, NULL);
3534                 if (err) {
3535                         LCONSOLE_WARN("Failed to remount %s %s (%d)\n",
3536                                       profilenm, read_only ?
3537                                       "read-only" : "read-write", err);
3538                         return err;
3539                 }
3540
3541                 if (read_only)
3542                         sb->s_flags |= SB_RDONLY;
3543                 else
3544                         sb->s_flags &= ~SB_RDONLY;
3545
3546                 if (test_bit(LL_SBI_VERBOSE, sbi->ll_flags))
3547                         LCONSOLE_WARN("Remounted %s %s\n", profilenm,
3548                                       read_only ?  "read-only" : "read-write");
3549         }
3550         return 0;
3551 }
3552
3553 /**
3554  * Cleanup the open handle that is cached on MDT-side.
3555  *
3556  * For open case, the client side open handling thread may hit error
3557  * after the MDT grant the open. Under such case, the client should
3558  * send close RPC to the MDT as cleanup; otherwise, the open handle
3559  * on the MDT will be leaked there until the client umount or evicted.
3560  *
3561  * In further, if someone unlinked the file, because the open handle
3562  * holds the reference on such file/object, then it will block the
3563  * subsequent threads that want to locate such object via FID.
3564  *
3565  * \param[in] sb        super block for this file-system
3566  * \param[in] open_req  pointer to the original open request
3567  */
3568 void ll_open_cleanup(struct super_block *sb, struct req_capsule *pill)
3569 {
3570         struct mdt_body                 *body;
3571         struct md_op_data               *op_data;
3572         struct ptlrpc_request           *close_req = NULL;
3573         struct obd_export               *exp       = ll_s2sbi(sb)->ll_md_exp;
3574         ENTRY;
3575
3576         body = req_capsule_server_get(pill, &RMF_MDT_BODY);
3577         OBD_ALLOC_PTR(op_data);
3578         if (op_data == NULL) {
3579                 CWARN("%s: cannot allocate op_data to release open handle for "
3580                       DFID"\n", ll_s2sbi(sb)->ll_fsname, PFID(&body->mbo_fid1));
3581
3582                 RETURN_EXIT;
3583         }
3584
3585         op_data->op_fid1 = body->mbo_fid1;
3586         op_data->op_open_handle = body->mbo_open_handle;
3587         op_data->op_mod_time = ktime_get_real_seconds();
3588         md_close(exp, op_data, NULL, &close_req);
3589         ptlrpc_req_finished(close_req);
3590         ll_finish_md_op_data(op_data);
3591
3592         EXIT;
3593 }
3594
3595 /* set filesystem-wide default LMV for subdir mount if it's enabled on ROOT. */
3596 static int ll_fileset_default_lmv_fixup(struct inode *inode,
3597                                         struct lustre_md *md)
3598 {
3599         struct ll_sb_info *sbi = ll_i2sbi(inode);
3600         struct ptlrpc_request *req = NULL;
3601         union lmv_mds_md *lmm = NULL;
3602         int size = 0;
3603         int rc;
3604
3605         LASSERT(is_root_inode(inode));
3606         LASSERT(!fid_is_root(&sbi->ll_root_fid));
3607         LASSERT(!md->def_lsm_obj);
3608
3609         rc = ll_dir_get_default_layout(inode, (void **)&lmm, &size, &req,
3610                                        OBD_MD_DEFAULT_MEA,
3611                                        GET_DEFAULT_LAYOUT_ROOT);
3612         if (rc && rc != -ENODATA)
3613                 GOTO(out, rc);
3614
3615         rc = 0;
3616         if (lmm && size) {
3617                 rc = md_stripe_object_create(sbi->ll_md_exp, &md->def_lsm_obj,
3618                                              lmm, size);
3619                 if (rc < 0)
3620                         GOTO(out, rc);
3621
3622                 rc = 0;
3623         }
3624         EXIT;
3625 out:
3626         if (req)
3627                 ptlrpc_req_finished(req);
3628         return rc;
3629 }
3630
3631 int ll_prep_inode(struct inode **inode, struct req_capsule *pill,
3632                   struct super_block *sb, struct lookup_intent *it)
3633 {
3634         struct ll_sb_info *sbi = NULL;
3635         struct lustre_md md = { NULL };
3636         bool default_lmv_deleted = false;
3637         int rc;
3638
3639         ENTRY;
3640
3641         LASSERT(*inode || sb);
3642         sbi = sb ? ll_s2sbi(sb) : ll_i2sbi(*inode);
3643         rc = md_get_lustre_md(sbi->ll_md_exp, pill, sbi->ll_dt_exp,
3644                               sbi->ll_md_exp, &md);
3645         if (rc != 0)
3646                 GOTO(out, rc);
3647
3648         /*
3649          * clear default_lmv only if intent_getattr reply doesn't contain it.
3650          * but it needs to be done after iget, check this early because
3651          * ll_update_lsm_md() may change md.
3652          */
3653         if (it && (it->it_op & (IT_LOOKUP | IT_GETATTR)) &&
3654             S_ISDIR(md.body->mbo_mode) && !md.def_lsm_obj) {
3655                 if (unlikely(*inode && is_root_inode(*inode) &&
3656                              !fid_is_root(&sbi->ll_root_fid))) {
3657                         rc = ll_fileset_default_lmv_fixup(*inode, &md);
3658                         if (rc)
3659                                 GOTO(out, rc);
3660                 }
3661
3662                 if (!md.def_lsm_obj)
3663                         default_lmv_deleted = true;
3664         }
3665
3666         if (*inode) {
3667                 rc = ll_update_inode(*inode, &md);
3668                 if (rc != 0)
3669                         GOTO(out, rc);
3670         } else {
3671                 bool api32 = test_bit(LL_SBI_32BIT_API, sbi->ll_flags);
3672                 struct lu_fid *fid1 = &md.body->mbo_fid1;
3673
3674                 LASSERT(sb != NULL);
3675
3676                 /*
3677                  * At this point server returns to client's same fid as client
3678                  * generated for creating. So using ->fid1 is okay here.
3679                  */
3680                 if (!fid_is_sane(fid1)) {
3681                         CERROR("%s: Fid is insane "DFID"\n",
3682                                 sbi->ll_fsname, PFID(fid1));
3683                         GOTO(out, rc = -EINVAL);
3684                 }
3685
3686                 *inode = ll_iget(sb, cl_fid_build_ino(fid1, api32), &md);
3687                 if (IS_ERR(*inode)) {
3688                         lmd_clear_acl(&md);
3689                         rc = IS_ERR(*inode) ? PTR_ERR(*inode) : -ENOMEM;
3690                         *inode = NULL;
3691                         CERROR("new_inode -fatal: rc %d\n", rc);
3692                         GOTO(out, rc);
3693                 }
3694         }
3695
3696         /* Handling piggyback layout lock.
3697          * Layout lock can be piggybacked by getattr and open request.
3698          * The lsm can be applied to inode only if it comes with a layout lock
3699          * otherwise correct layout may be overwritten, for example:
3700          * 1. proc1: mdt returns a lsm but not granting layout
3701          * 2. layout was changed by another client
3702          * 3. proc2: refresh layout and layout lock granted
3703          * 4. proc1: to apply a stale layout */
3704         if (it != NULL && it->it_lock_mode != 0) {
3705                 struct lustre_handle lockh;
3706                 struct ldlm_lock *lock;
3707
3708                 lockh.cookie = it->it_lock_handle;
3709                 lock = ldlm_handle2lock(&lockh);
3710                 LASSERT(lock != NULL);
3711                 if (ldlm_has_layout(lock)) {
3712                         struct cl_object_conf conf;
3713
3714                         memset(&conf, 0, sizeof(conf));
3715                         conf.coc_opc = OBJECT_CONF_SET;
3716                         conf.coc_inode = *inode;
3717                         conf.coc_lock = lock;
3718                         conf.coc_try = false;
3719                         conf.u.coc_layout = md.layout;
3720                         (void)ll_layout_conf(*inode, &conf);
3721                 }
3722                 LDLM_LOCK_PUT(lock);
3723         }
3724
3725         if (default_lmv_deleted)
3726                 ll_update_default_lsm_md(*inode, &md);
3727
3728         /* we may want to apply some policy for foreign file/dir */
3729         if (ll_sbi_has_foreign_symlink(sbi)) {
3730                 rc = ll_manage_foreign(*inode, &md);
3731                 if (rc < 0)
3732                         GOTO(out, rc);
3733         }
3734
3735         GOTO(out, rc = 0);
3736
3737 out:
3738         /* cleanup will be done if necessary */
3739         md_put_lustre_md(sbi->ll_md_exp, &md);
3740
3741         if (rc != 0 && it != NULL && it->it_op & IT_OPEN) {
3742                 ll_intent_drop_lock(it);
3743                 ll_open_cleanup(sb != NULL ? sb : (*inode)->i_sb, pill);
3744         }
3745
3746         return rc;
3747 }
3748
3749 int ll_obd_statfs(struct inode *inode, void __user *uarg)
3750 {
3751         struct ll_sb_info *sbi = NULL;
3752         struct obd_export *exp;
3753         struct obd_ioctl_data *data = NULL;
3754         __u32 type;
3755         int len = 0, rc;
3756
3757         if (inode)
3758                 sbi = ll_i2sbi(inode);
3759         if (!sbi)
3760                 GOTO(out_statfs, rc = -EINVAL);
3761
3762         rc = obd_ioctl_getdata(&data, &len, uarg);
3763         if (rc)
3764                 GOTO(out_statfs, rc);
3765
3766         if (!data->ioc_inlbuf1 || !data->ioc_inlbuf2 ||
3767             !data->ioc_pbuf1 || !data->ioc_pbuf2)
3768                 GOTO(out_statfs, rc = -EINVAL);
3769
3770         if (data->ioc_inllen1 != sizeof(__u32) ||
3771             data->ioc_inllen2 != sizeof(__u32) ||
3772             data->ioc_plen1 != sizeof(struct obd_statfs) ||
3773             data->ioc_plen2 != sizeof(struct obd_uuid))
3774                 GOTO(out_statfs, rc = -EINVAL);
3775
3776         memcpy(&type, data->ioc_inlbuf1, sizeof(__u32));
3777         if (type & LL_STATFS_LMV)
3778                 exp = sbi->ll_md_exp;
3779         else if (type & LL_STATFS_LOV)
3780                 exp = sbi->ll_dt_exp;
3781         else
3782                 GOTO(out_statfs, rc = -ENODEV);
3783
3784         rc = obd_iocontrol(IOC_OBD_STATFS, exp, len, data, NULL);
3785         if (rc)
3786                 GOTO(out_statfs, rc);
3787 out_statfs:
3788         OBD_FREE_LARGE(data, len);
3789         return rc;
3790 }
3791
3792 /*
3793  * this is normally called in ll_fini_md_op_data(), but sometimes it needs to
3794  * be called early to avoid deadlock.
3795  */
3796 void ll_unlock_md_op_lsm(struct md_op_data *op_data)
3797 {
3798         lmv_stripe_object_put(&op_data->op_lso2);
3799         lmv_stripe_object_put(&op_data->op_lso1);
3800         lmv_stripe_object_put(&op_data->op_default_lso1);
3801 }
3802
3803 /* this function prepares md_op_data hint for passing it down to MD stack. */
3804 struct md_op_data *ll_prep_md_op_data(struct md_op_data *op_data,
3805                                       struct inode *i1, struct inode *i2,
3806                                       const char *name, size_t namelen,
3807                                       __u32 mode, enum md_op_code opc,
3808                                       void *data)
3809 {
3810         struct llcrypt_name fname = { 0 };
3811         int rc;
3812
3813         LASSERT(i1 != NULL);
3814
3815         if (name == NULL) {
3816                 /* Do not reuse namelen for something else. */
3817                 if (namelen != 0)
3818                         return ERR_PTR(-EINVAL);
3819         } else {
3820                 if ((!IS_ENCRYPTED(i1) ||
3821                      (opc != LUSTRE_OPC_LOOKUP && opc != LUSTRE_OPC_CREATE)) &&
3822                     namelen > ll_i2sbi(i1)->ll_namelen)
3823                         return ERR_PTR(-ENAMETOOLONG);
3824
3825                 /* "/" is not valid name, but it's allowed */
3826                 if (!lu_name_is_valid_2(name, namelen) &&
3827                     strncmp("/", name, namelen) != 0)
3828                         return ERR_PTR(-EINVAL);
3829         }
3830
3831         if (op_data == NULL)
3832                 OBD_ALLOC_PTR(op_data);
3833
3834         if (op_data == NULL)
3835                 return ERR_PTR(-ENOMEM);
3836
3837         ll_i2gids(op_data->op_suppgids, i1, i2);
3838         /* If the client is using a subdir mount and looks at what it sees as
3839          * /.fscrypt, interpret it as the .fscrypt dir at the root of the fs.
3840          */
3841         if (unlikely(i1->i_sb && i1->i_sb->s_root && is_root_inode(i1) &&
3842                      !fid_is_root(ll_inode2fid(i1)) &&
3843                      name && namelen == strlen(dot_fscrypt_name) &&
3844                      strncmp(name, dot_fscrypt_name, namelen) == 0))
3845                 lu_root_fid(&op_data->op_fid1);
3846         else
3847                 op_data->op_fid1 = *ll_inode2fid(i1);
3848
3849         if (S_ISDIR(i1->i_mode)) {
3850                 down_read(&ll_i2info(i1)->lli_lsm_sem);
3851                 op_data->op_lso1 =
3852                         lmv_stripe_object_get(ll_i2info(i1)->lli_lsm_obj);
3853                 op_data->op_default_lso1 =
3854                         lmv_stripe_object_get(ll_i2info(i1)->lli_def_lsm_obj);
3855                 up_read(&ll_i2info(i1)->lli_lsm_sem);
3856         }
3857
3858         if (i2) {
3859                 op_data->op_fid2 = *ll_inode2fid(i2);
3860                 if (S_ISDIR(i2->i_mode)) {
3861                         down_read(&ll_i2info(i2)->lli_lsm_sem);
3862                         op_data->op_lso2 =
3863                             lmv_stripe_object_get(ll_i2info(i2)->lli_lsm_obj);
3864                         up_read(&ll_i2info(i2)->lli_lsm_sem);
3865                 }
3866         } else {
3867                 fid_zero(&op_data->op_fid2);
3868         }
3869
3870         if (test_bit(LL_SBI_64BIT_HASH, ll_i2sbi(i1)->ll_flags))
3871                 op_data->op_cli_flags |= CLI_HASH64;
3872
3873         if (ll_need_32bit_api(ll_i2sbi(i1)))
3874                 op_data->op_cli_flags |= CLI_API32;
3875
3876         if ((i2 && is_root_inode(i2)) ||
3877             opc == LUSTRE_OPC_LOOKUP || opc == LUSTRE_OPC_CREATE) {
3878                 /* In case of lookup, ll_setup_filename() has already been
3879                  * called in ll_lookup_it(), so just take provided name.
3880                  * Also take provided name if we are dealing with root inode.
3881                  */
3882                 fname.disk_name.name = (unsigned char *)name;
3883                 fname.disk_name.len = namelen;
3884         } else if (name && namelen) {
3885                 struct qstr dname = QSTR_INIT(name, namelen);
3886                 struct inode *dir;
3887                 struct lu_fid *pfid = NULL;
3888                 struct lu_fid fid;
3889                 int lookup;
3890
3891                 if (!S_ISDIR(i1->i_mode) && i2 && S_ISDIR(i2->i_mode)) {
3892                         /* special case when called from ll_link() */
3893                         dir = i2;
3894                         lookup = 0;
3895                 } else {
3896                         dir = i1;
3897                         lookup = (int)(opc == LUSTRE_OPC_ANY);
3898                 }
3899                 if (opc == LUSTRE_OPC_ANY && lookup)
3900                         pfid = &fid;
3901                 rc = ll_setup_filename(dir, &dname, lookup, &fname, pfid);
3902                 if (rc) {
3903                         ll_finish_md_op_data(op_data);
3904                         return ERR_PTR(rc);
3905                 }
3906                 if (pfid && !fid_is_zero(pfid)) {
3907                         if (i2 == NULL)
3908                                 op_data->op_fid2 = fid;
3909                         op_data->op_bias = MDS_FID_OP;
3910                 }
3911                 if (fname.disk_name.name &&
3912                     fname.disk_name.name != (unsigned char *)name) {
3913                         /* op_data->op_name must be freed after use */
3914                         op_data->op_flags |= MF_OPNAME_KMALLOCED;
3915                 }
3916         }
3917
3918         /* In fact LUSTRE_OPC_LOOKUP, LUSTRE_OPC_OPEN
3919          * are LUSTRE_OPC_ANY
3920          */
3921         if (opc == LUSTRE_OPC_LOOKUP || opc == LUSTRE_OPC_OPEN)
3922                 op_data->op_code = LUSTRE_OPC_ANY;
3923         else
3924                 op_data->op_code = opc;
3925         op_data->op_name = fname.disk_name.name;
3926         op_data->op_namelen = fname.disk_name.len;
3927         op_data->op_mode = mode;
3928         op_data->op_mod_time = ktime_get_real_seconds();
3929         op_data->op_fsuid = from_kuid(&init_user_ns, current_fsuid());
3930         op_data->op_fsgid = from_kgid(&init_user_ns, current_fsgid());
3931         op_data->op_cap = current_cap();
3932         op_data->op_mds = 0;
3933         if ((opc == LUSTRE_OPC_CREATE) && (name != NULL) &&
3934              filename_is_volatile(name, namelen, &op_data->op_mds)) {
3935                 op_data->op_bias |= MDS_CREATE_VOLATILE;
3936         }
3937         op_data->op_data = data;
3938
3939         return op_data;
3940 }
3941
3942 void ll_finish_md_op_data(struct md_op_data *op_data)
3943 {
3944         ll_unlock_md_op_lsm(op_data);
3945         ll_security_release_secctx(op_data->op_file_secctx,
3946                                    op_data->op_file_secctx_size,
3947                                    op_data->op_file_secctx_slot);
3948         if (op_data->op_flags & MF_OPNAME_KMALLOCED)
3949                 /* allocated via ll_setup_filename called
3950                  * from ll_prep_md_op_data
3951                  */
3952                 kfree(op_data->op_name);
3953         llcrypt_free_ctx(op_data->op_file_encctx, op_data->op_file_encctx_size);
3954         OBD_FREE_PTR(op_data);
3955 }
3956
3957 int ll_show_options(struct seq_file *seq, struct dentry *dentry)
3958 {
3959         struct ll_sb_info *sbi;
3960         int i;
3961
3962         LASSERT(seq && dentry);
3963         sbi = ll_s2sbi(dentry->d_sb);
3964
3965         if (test_bit(LL_SBI_NOLCK, sbi->ll_flags))
3966                 seq_puts(seq, "nolock");
3967
3968         for (i = 1; ll_sbi_flags_name[i].token != LL_SBI_NUM_MOUNT_OPT; i++) {
3969                 /* match_table in some cases has patterns for both enabled and
3970                  * disabled cases. Ignore 'no'xxx versions if bit is set.
3971                  */
3972                 if (test_bit(ll_sbi_flags_name[i].token, sbi->ll_flags) &&
3973                     strncmp(ll_sbi_flags_name[i].pattern, "no", 2)) {
3974                         if (ll_sbi_flags_name[i].token ==
3975                             LL_SBI_FOREIGN_SYMLINK) {
3976                                 seq_show_option(seq, "foreign_symlink",
3977                                                 sbi->ll_foreign_symlink_prefix);
3978                         } else {
3979                                 seq_printf(seq, ",%s",
3980                                            ll_sbi_flags_name[i].pattern);
3981                         }
3982
3983                         /* You can have either localflock or flock but not
3984                          * both. If localflock is set don't print flock or
3985                          * noflock.
3986                          */
3987                         if (ll_sbi_flags_name[i].token == LL_SBI_LOCALFLOCK)
3988                                 i += 2;
3989                 } else if (!test_bit(ll_sbi_flags_name[i].token, sbi->ll_flags) &&
3990                            !strncmp(ll_sbi_flags_name[i].pattern, "no", 2)) {
3991                         seq_printf(seq, ",%s",
3992                                    ll_sbi_flags_name[i].pattern);
3993                 }
3994         }
3995
3996         llcrypt_show_test_dummy_encryption(seq, ',', dentry->d_sb);
3997
3998         RETURN(0);
3999 }
4000
4001 /**
4002  * Get obd name by cmd, and copy out to user space
4003  */
4004 int ll_get_obd_name(struct inode *inode, unsigned int cmd, void __user *uarg)
4005 {
4006         struct ll_sb_info *sbi = ll_i2sbi(inode);
4007         struct obd_device *obd;
4008         ENTRY;
4009
4010         if (cmd == OBD_IOC_GETNAME_OLD || cmd == OBD_IOC_GETDTNAME)
4011                 obd = class_exp2obd(sbi->ll_dt_exp);
4012         else if (cmd == OBD_IOC_GETMDNAME)
4013                 obd = class_exp2obd(sbi->ll_md_exp);
4014         else
4015                 RETURN(-EINVAL);
4016
4017         if (!obd)
4018                 RETURN(-ENOENT);
4019
4020         if (copy_to_user(uarg, obd->obd_name, strlen(obd->obd_name) + 1))
4021                 RETURN(-EFAULT);
4022
4023         RETURN(0);
4024 }
4025
4026 struct dname_buf {
4027         struct work_struct db_work;
4028         struct dentry *db_dentry;
4029         /* Let's hope the path is not too long, 32 bytes for the work struct
4030          * on my kernel
4031          */
4032         char buf[PAGE_SIZE - sizeof(struct work_struct) - sizeof(void *)];
4033 };
4034
4035 static void ll_dput_later(struct work_struct *work)
4036 {
4037         struct dname_buf *db = container_of(work, struct dname_buf, db_work);
4038
4039         dput(db->db_dentry);
4040         free_page((unsigned long)db);
4041 }
4042
4043 static char* ll_d_path(struct dentry *dentry, char *buf, int bufsize)
4044 {
4045         char *path = NULL;
4046
4047         struct path p;
4048
4049         p.dentry = dentry;
4050         p.mnt = current->fs->root.mnt;
4051         path_get(&p);
4052         path = d_path(&p, buf, bufsize);
4053         path_put(&p);
4054         return path;
4055 }
4056
4057 void ll_dirty_page_discard_warn(struct inode *inode, int ioret)
4058 {
4059         struct dname_buf *db;
4060         char  *path = NULL;
4061         struct dentry *dentry = NULL;
4062
4063         /* this can be called inside spin lock so use GFP_ATOMIC. */
4064         db = (struct dname_buf *)__get_free_page(GFP_ATOMIC);
4065         if (db != NULL) {
4066
4067                 dentry = d_find_alias(inode);
4068                 if (dentry != NULL)
4069                         path = ll_d_path(dentry, db->buf, sizeof(db->buf));
4070         }
4071
4072         /* The below message is checked in recovery-small.sh test_24b */
4073         CDEBUG(D_WARNING,
4074                "%s: dirty page discard: %s/fid: "DFID"/%s may get corrupted "
4075                "(rc %d)\n", ll_i2sbi(inode)->ll_fsname,
4076                s2lsi(inode->i_sb)->lsi_lmd->lmd_dev,
4077                PFID(ll_inode2fid(inode)),
4078                (path && !IS_ERR(path)) ? path : "", ioret);
4079
4080         if (dentry != NULL) {
4081                 /* We cannot dput here since if we happen to be the last holder
4082                  * then we can end up waiting for page evictions that
4083                  * in turn wait for RPCs that need this instance of ptlrpcd
4084                  * (callng brw_interpret->*page_completion*->vmpage_error->here)
4085                  * LU-15340
4086                  */
4087                 INIT_WORK(&db->db_work, ll_dput_later);
4088                 db->db_dentry = dentry;
4089                 schedule_work(&db->db_work);
4090         } else {
4091                 if (db != NULL)
4092                         free_page((unsigned long)db);
4093         }
4094 }
4095
4096 ssize_t ll_copy_user_md(const struct lov_user_md __user *md,
4097                         struct lov_user_md **kbuf)
4098 {
4099         struct lov_user_md      lum;
4100         ssize_t                 lum_size;
4101         ENTRY;
4102
4103         if (copy_from_user(&lum, md, sizeof(lum)))
4104                 RETURN(-EFAULT);
4105
4106         lum_size = ll_lov_user_md_size(&lum);
4107         if (lum_size < 0)
4108                 RETURN(lum_size);
4109
4110         OBD_ALLOC_LARGE(*kbuf, lum_size);
4111         if (*kbuf == NULL)
4112                 RETURN(-ENOMEM);
4113
4114         if (copy_from_user(*kbuf, md, lum_size) != 0) {
4115                 OBD_FREE_LARGE(*kbuf, lum_size);
4116                 RETURN(-EFAULT);
4117         }
4118
4119         RETURN(lum_size);
4120 }
4121
4122 /*
4123  * Compute llite root squash state after a change of root squash
4124  * configuration setting or add/remove of a lnet nid
4125  */
4126 void ll_compute_rootsquash_state(struct ll_sb_info *sbi)
4127 {
4128         struct root_squash_info *squash = &sbi->ll_squash;
4129         int i;
4130         bool matched;
4131         struct lnet_processid id;
4132
4133         /* Update norootsquash flag */
4134         spin_lock(&squash->rsi_lock);
4135         if (list_empty(&squash->rsi_nosquash_nids))
4136                 clear_bit(LL_SBI_NOROOTSQUASH, sbi->ll_flags);
4137         else {
4138                 /* Do not apply root squash as soon as one of our NIDs is
4139                  * in the nosquash_nids list
4140                  */
4141                 struct lustre_sb_info *lsi = sbi->lsi;
4142                 bool large_nid = false;
4143
4144                 if (exp_connect_flags2(lsi->lsi_mgc->u.cli.cl_mgc_mgsexp) &
4145                       OBD_CONNECT2_LARGE_NID)
4146                         large_nid = true;
4147
4148                 matched = false;
4149                 i = 0;
4150                 while (LNetGetId(i++, &id, large_nid) != -ENOENT) {
4151                         if (nid_is_lo0(&id.nid))
4152                                 continue;
4153                         if (cfs_match_nid(&id.nid,
4154                                           &squash->rsi_nosquash_nids)) {
4155                                 matched = true;
4156                                 break;
4157                         }
4158                 }
4159                 if (matched)
4160                         set_bit(LL_SBI_NOROOTSQUASH, sbi->ll_flags);
4161                 else
4162                         clear_bit(LL_SBI_NOROOTSQUASH, sbi->ll_flags);
4163         }
4164         spin_unlock(&squash->rsi_lock);
4165 }
4166
4167 /**
4168  * Parse linkea content to extract information about a given hardlink
4169  *
4170  * \param[in]   ldata      - Initialized linkea data
4171  * \param[in]   linkno     - Link identifier
4172  * \param[out]  parent_fid - The entry's parent FID
4173  * \param[out]  ln         - Entry name destination buffer
4174  *
4175  * \retval 0 on success
4176  * \retval Appropriate negative error code on failure
4177  */
4178 static int ll_linkea_decode(struct linkea_data *ldata, unsigned int linkno,
4179                             struct lu_fid *parent_fid, struct lu_name *ln)
4180 {
4181         unsigned int    idx;
4182         int             rc;
4183         ENTRY;
4184
4185         rc = linkea_init_with_rec(ldata);
4186         if (rc < 0)
4187                 RETURN(rc);
4188
4189         if (linkno >= ldata->ld_leh->leh_reccount)
4190                 /* beyond last link */
4191                 RETURN(-ENODATA);
4192
4193         linkea_first_entry(ldata);
4194         for (idx = 0; ldata->ld_lee != NULL; idx++) {
4195                 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen, ln,
4196                                     parent_fid);
4197                 if (idx == linkno)
4198                         break;
4199
4200                 linkea_next_entry(ldata);
4201         }
4202
4203         if (idx < linkno)
4204                 RETURN(-ENODATA);
4205
4206         RETURN(0);
4207 }
4208
4209 /**
4210  * Get parent FID and name of an identified link. Operation is performed for
4211  * a given link number, letting the caller iterate over linkno to list one or
4212  * all links of an entry.
4213  *
4214  * \param[in]     file - File descriptor against which to perform the operation
4215  * \param[in,out] arg  - User-filled structure containing the linkno to operate
4216  *                       on and the available size. It is eventually filled with
4217  *                       the requested information or left untouched on error
4218  *
4219  * \retval - 0 on success
4220  * \retval - Appropriate negative error code on failure
4221  */
4222 int ll_getparent(struct file *file, struct getparent __user *arg)
4223 {
4224         struct inode            *inode = file_inode(file);
4225         struct linkea_data      *ldata;
4226         struct lu_buf            buf = LU_BUF_NULL;
4227         struct lu_name           ln;
4228         struct lu_fid            parent_fid;
4229         __u32                    linkno;
4230         __u32                    name_size;
4231         int                      rc;
4232
4233         ENTRY;
4234
4235         if (!capable(CAP_DAC_READ_SEARCH) &&
4236             !test_bit(LL_SBI_USER_FID2PATH, ll_i2sbi(inode)->ll_flags))
4237                 RETURN(-EPERM);
4238
4239         if (get_user(name_size, &arg->gp_name_size))
4240                 RETURN(-EFAULT);
4241
4242         if (get_user(linkno, &arg->gp_linkno))
4243                 RETURN(-EFAULT);
4244
4245         if (name_size > PATH_MAX)
4246                 RETURN(-EINVAL);
4247
4248         OBD_ALLOC(ldata, sizeof(*ldata));
4249         if (ldata == NULL)
4250                 RETURN(-ENOMEM);
4251
4252         rc = linkea_data_new(ldata, &buf);
4253         if (rc < 0)
4254                 GOTO(ldata_free, rc);
4255
4256         rc = ll_xattr_list(inode, XATTR_NAME_LINK, XATTR_TRUSTED_T, buf.lb_buf,
4257                            buf.lb_len, OBD_MD_FLXATTR);
4258         if (rc < 0)
4259                 GOTO(lb_free, rc);
4260
4261         rc = ll_linkea_decode(ldata, linkno, &parent_fid, &ln);
4262         if (rc < 0)
4263                 GOTO(lb_free, rc);
4264
4265         if (ln.ln_namelen >= name_size)
4266                 GOTO(lb_free, rc = -EOVERFLOW);
4267
4268         if (copy_to_user(&arg->gp_fid, &parent_fid, sizeof(arg->gp_fid)))
4269                 GOTO(lb_free, rc = -EFAULT);
4270
4271         if (copy_to_user(&arg->gp_name, ln.ln_name, ln.ln_namelen))
4272                 GOTO(lb_free, rc = -EFAULT);
4273
4274         if (put_user('\0', arg->gp_name + ln.ln_namelen))
4275                 GOTO(lb_free, rc = -EFAULT);
4276
4277 lb_free:
4278         lu_buf_free(&buf);
4279 ldata_free:
4280         OBD_FREE(ldata, sizeof(*ldata));
4281
4282         RETURN(rc);
4283 }