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