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