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