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