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