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