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