Whamcloud - gitweb
LU-16517 build: pass extra configure options to "make debs"
[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 }
1278
1279 #define MAX_STRING_SIZE 128
1280
1281 #ifndef HAVE_SUPER_SETUP_BDI_NAME
1282 #ifndef HAVE_BDI_CAP_MAP_COPY
1283 # define BDI_CAP_MAP_COPY       0
1284 #endif
1285
1286 static int super_setup_bdi_name(struct super_block *sb, char *fmt, ...)
1287 {
1288         struct  lustre_sb_info *lsi = s2lsi(sb);
1289         char buf[MAX_STRING_SIZE];
1290         va_list args;
1291         int err;
1292
1293         err = bdi_init(&lsi->lsi_bdi);
1294         if (err)
1295                 return err;
1296
1297         lsi->lsi_flags |= LSI_BDI_INITIALIZED;
1298         lsi->lsi_bdi.capabilities = BDI_CAP_MAP_COPY;
1299         lsi->lsi_bdi.name = "lustre";
1300         va_start(args, fmt);
1301         vsnprintf(buf, MAX_STRING_SIZE, fmt, args);
1302         va_end(args);
1303         err = bdi_register(&lsi->lsi_bdi, NULL, "%s", buf);
1304         va_end(args);
1305         if (!err)
1306                 sb->s_bdi = &lsi->lsi_bdi;
1307
1308         return err;
1309 }
1310 #endif /* !HAVE_SUPER_SETUP_BDI_NAME */
1311
1312 int ll_fill_super(struct super_block *sb)
1313 {
1314         struct  lustre_profile *lprof = NULL;
1315         struct  lustre_sb_info *lsi = s2lsi(sb);
1316         struct  ll_sb_info *sbi = NULL;
1317         char    *dt = NULL, *md = NULL;
1318         char    *profilenm = get_profile_name(sb);
1319         struct config_llog_instance *cfg;
1320         /* %p for void* in printf needs 16+2 characters: 0xffffffffffffffff */
1321         const int instlen = LUSTRE_MAXINSTANCE + 2;
1322         unsigned long cfg_instance = ll_get_cfg_instance(sb);
1323         char name[MAX_STRING_SIZE];
1324         int md_len = 0;
1325         int dt_len = 0;
1326         uuid_t uuid;
1327         char *ptr;
1328         int len;
1329         int err;
1330
1331         ENTRY;
1332         /* for ASLR, to map between cfg_instance and hashed ptr */
1333         CDEBUG(D_VFSTRACE, "VFS Op: cfg_instance %s-%016lx (sb %p)\n",
1334                profilenm, cfg_instance, sb);
1335
1336         CFS_RACE(OBD_FAIL_LLITE_RACE_MOUNT);
1337
1338         OBD_ALLOC_PTR(cfg);
1339         if (cfg == NULL)
1340                 GOTO(out_free_cfg, err = -ENOMEM);
1341
1342         /* client additional sb info */
1343         lsi->lsi_llsbi = sbi = ll_init_sbi(lsi);
1344         if (IS_ERR(sbi))
1345                 GOTO(out_free_cfg, err = PTR_ERR(sbi));
1346
1347         err = ll_options(lsi->lsi_lmd->lmd_opts, sb);
1348         if (err)
1349                 GOTO(out_free_cfg, err);
1350
1351         /* LSI_FILENAME_ENC is only used by embedded llcrypt */
1352 #ifdef CONFIG_LL_ENCRYPTION
1353         if (ll_sb_has_test_dummy_encryption(sb))
1354                 /* enable filename encryption by default for dummy enc mode */
1355                 lsi->lsi_flags |= LSI_FILENAME_ENC;
1356         else
1357                 /* filename encryption is disabled by default */
1358                 lsi->lsi_flags &= ~LSI_FILENAME_ENC;
1359 #endif
1360
1361         /* kernel >= 2.6.38 store dentry operations in sb->s_d_op. */
1362         sb->s_d_op = &ll_d_ops;
1363
1364         /* UUID handling */
1365         generate_random_uuid(uuid.b);
1366         snprintf(sbi->ll_sb_uuid.uuid, sizeof(sbi->ll_sb_uuid), "%pU", uuid.b);
1367
1368         CDEBUG(D_CONFIG, "llite sb uuid: %s\n", sbi->ll_sb_uuid.uuid);
1369
1370         /* Get fsname */
1371         len = strlen(profilenm);
1372         ptr = strrchr(profilenm, '-');
1373         if (ptr && (strcmp(ptr, "-client") == 0))
1374                 len -= 7;
1375
1376         if (len > LUSTRE_MAXFSNAME) {
1377                 if (unlikely(len >= MAX_STRING_SIZE))
1378                         len = MAX_STRING_SIZE - 1;
1379                 strncpy(name, profilenm, len);
1380                 name[len] = '\0';
1381                 err = -ENAMETOOLONG;
1382                 CERROR("%s: fsname longer than %u characters: rc = %d\n",
1383                        name, LUSTRE_MAXFSNAME, err);
1384                 GOTO(out_free_cfg, err);
1385         }
1386         strncpy(sbi->ll_fsname, profilenm, len);
1387         sbi->ll_fsname[len] = '\0';
1388
1389         /* Mount info */
1390         snprintf(name, sizeof(name), "%.*s-%016lx", len,
1391                  profilenm, cfg_instance);
1392
1393         err = super_setup_bdi_name(sb, "%s", name);
1394         if (err)
1395                 GOTO(out_free_cfg, err);
1396
1397         /* disable kernel readahead */
1398         sb->s_bdi->ra_pages = 0;
1399 #ifdef HAVE_BDI_IO_PAGES
1400         sb->s_bdi->io_pages = 0;
1401 #endif
1402
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                 inode_lock(inode);
1995         rc = simple_setattr(&init_user_ns, dentry, &op_data->op_attr);
1996         if (S_ISREG(inode->i_mode))
1997                 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 int ll_io_zero_page(struct inode *inode, pgoff_t index, pgoff_t offset,
2026                     unsigned 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         CDEBUG(D_VFSTRACE, "%s: setattr inode "DFID"(%p) from %llu to %llu, "
2228                "valid %x, hsm_import %d\n",
2229                ll_i2sbi(inode)->ll_fsname, PFID(&lli->lli_fid),
2230                inode, i_size_read(inode), attr->ia_size, attr->ia_valid,
2231                hsm_import);
2232
2233         if (attr->ia_valid & ATTR_SIZE) {
2234                 /* Check new size against VFS/VM file size limit and rlimit */
2235                 rc = inode_newsize_ok(inode, attr->ia_size);
2236                 if (rc)
2237                         RETURN(rc);
2238
2239                 /* The maximum Lustre file size is variable, based on the
2240                  * OST maximum object size and number of stripes.  This
2241                  * needs another check in addition to the VFS check above. */
2242                 if (attr->ia_size > ll_file_maxbytes(inode)) {
2243                         CDEBUG(D_INODE,"file "DFID" too large %llu > %llu\n",
2244                                PFID(&lli->lli_fid), attr->ia_size,
2245                                ll_file_maxbytes(inode));
2246                         RETURN(-EFBIG);
2247                 }
2248
2249                 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
2250         }
2251
2252         /* POSIX: check before ATTR_*TIME_SET set (from inode_change_ok) */
2253         if (attr->ia_valid & TIMES_SET_FLAGS) {
2254                 if ((!uid_eq(current_fsuid(), inode->i_uid)) &&
2255                     !capable(CAP_FOWNER))
2256                         RETURN(-EPERM);
2257         }
2258
2259         /* We mark all of the fields "set" so MDS/OST does not re-set them */
2260         if (!(xvalid & OP_XVALID_CTIME_SET) &&
2261              (attr->ia_valid & ATTR_CTIME)) {
2262                 attr->ia_ctime = current_time(inode);
2263                 xvalid |= OP_XVALID_CTIME_SET;
2264         }
2265         if (!(attr->ia_valid & ATTR_ATIME_SET) &&
2266             (attr->ia_valid & ATTR_ATIME)) {
2267                 attr->ia_atime = current_time(inode);
2268                 attr->ia_valid |= ATTR_ATIME_SET;
2269         }
2270         if (!(attr->ia_valid & ATTR_MTIME_SET) &&
2271             (attr->ia_valid & ATTR_MTIME)) {
2272                 attr->ia_mtime = current_time(inode);
2273                 attr->ia_valid |= ATTR_MTIME_SET;
2274         }
2275
2276         if (attr->ia_valid & (ATTR_MTIME | ATTR_CTIME))
2277                 CDEBUG(D_INODE, "setting mtime %lld, ctime %lld, now = %lld\n",
2278                        (s64)attr->ia_mtime.tv_sec, (s64)attr->ia_ctime.tv_sec,
2279                        ktime_get_real_seconds());
2280
2281         if (S_ISREG(inode->i_mode))
2282                 inode_unlock(inode);
2283
2284         /* We always do an MDS RPC, even if we're only changing the size;
2285          * only the MDS knows whether truncate() should fail with -ETXTBUSY */
2286
2287         OBD_ALLOC_PTR(op_data);
2288         if (op_data == NULL)
2289                 GOTO(out, rc = -ENOMEM);
2290
2291         if (!hsm_import && attr->ia_valid & ATTR_SIZE) {
2292                 /* If we are changing file size, file content is
2293                  * modified, flag it.
2294                  */
2295                 xvalid |= OP_XVALID_OWNEROVERRIDE;
2296                 op_data->op_bias |= MDS_DATA_MODIFIED;
2297                 clear_bit(LLIF_DATA_MODIFIED, &lli->lli_flags);
2298         }
2299
2300         if (attr->ia_valid & ATTR_FILE) {
2301                 struct ll_file_data *fd = attr->ia_file->private_data;
2302
2303                 if (fd->fd_lease_och)
2304                         op_data->op_bias |= MDS_TRUNC_KEEP_LEASE;
2305         }
2306
2307         op_data->op_attr = *attr;
2308         op_data->op_xvalid = xvalid;
2309
2310         rc = ll_md_setattr(dentry, op_data);
2311         if (rc)
2312                 GOTO(out, rc);
2313         lli->lli_synced_to_mds = false;
2314
2315         if (!S_ISREG(inode->i_mode) || hsm_import)
2316                 GOTO(out, rc = 0);
2317
2318         if (attr->ia_valid & (ATTR_SIZE | ATTR_ATIME | ATTR_ATIME_SET |
2319                               ATTR_MTIME | ATTR_MTIME_SET | ATTR_CTIME) ||
2320             xvalid & OP_XVALID_CTIME_SET) {
2321                 bool cached = false;
2322
2323                 rc = pcc_inode_setattr(inode, attr, &cached);
2324                 if (cached) {
2325                         if (rc) {
2326                                 CERROR("%s: PCC inode "DFID" setattr failed: "
2327                                        "rc = %d\n",
2328                                        ll_i2sbi(inode)->ll_fsname,
2329                                        PFID(&lli->lli_fid), rc);
2330                                 GOTO(out, rc);
2331                         }
2332                 } else {
2333                         unsigned int flags = 0;
2334
2335                         /* For truncate and utimes sending attributes to OSTs,
2336                          * setting mtime/atime to the past will be performed
2337                          * under PW [0:EOF] extent lock (new_size:EOF for
2338                          * truncate). It may seem excessive to send mtime/atime
2339                          * updates to OSTs when not setting times to past, but
2340                          * it is necessary due to possible time
2341                          * de-synchronization between MDT inode and OST objects
2342                          */
2343                         if (S_ISREG(inode->i_mode) && IS_ENCRYPTED(inode)) {
2344                                 xvalid |= OP_XVALID_FLAGS;
2345                                 flags = LUSTRE_ENCRYPT_FL;
2346                                 /* Call to ll_io_zero_page is not necessary if
2347                                  * truncating on PAGE_SIZE boundary, because
2348                                  * whole pages will be wiped.
2349                                  * In case of Direct IO, all we need is to set
2350                                  * new size.
2351                                  */
2352                                 if (attr->ia_valid & ATTR_SIZE &&
2353                                     attr->ia_size & ~PAGE_MASK &&
2354                                     !(attr->ia_valid & ATTR_FILE &&
2355                                       attr->ia_file->f_flags & O_DIRECT)) {
2356                                         pgoff_t offset =
2357                                                 attr->ia_size & (PAGE_SIZE - 1);
2358
2359                                         rc = ll_io_zero_page(inode,
2360                                                     attr->ia_size >> PAGE_SHIFT,
2361                                                     offset, PAGE_SIZE - offset);
2362                                         if (rc)
2363                                                 GOTO(out, rc);
2364                                 }
2365                                 /* If encrypted volatile file without the key,
2366                                  * we need to fetch size from reference file,
2367                                  * and set it on OST objects. This happens when
2368                                  * migrating or extending an encrypted file
2369                                  * without the key.
2370                                  */
2371                                 if (filename_is_volatile(dentry->d_name.name,
2372                                                          dentry->d_name.len,
2373                                                          NULL) &&
2374                                     llcrypt_require_key(inode) == -ENOKEY) {
2375                                         struct file *ref_file;
2376                                         struct inode *ref_inode;
2377                                         struct ll_inode_info *ref_lli;
2378                                         struct cl_object *ref_obj;
2379                                         struct cl_attr ref_attr = { 0 };
2380                                         struct lu_env *env;
2381                                         __u16 refcheck;
2382
2383                                         rc = volatile_ref_file(
2384                                                 dentry->d_name.name,
2385                                                 dentry->d_name.len,
2386                                                 &ref_file);
2387                                         if (rc)
2388                                                 GOTO(out, rc);
2389
2390                                         ref_inode = file_inode(ref_file);
2391                                         if (!ref_inode) {
2392                                                 fput(ref_file);
2393                                                 GOTO(out, rc = -EINVAL);
2394                                         }
2395
2396                                         env = cl_env_get(&refcheck);
2397                                         if (IS_ERR(env))
2398                                                 GOTO(out, rc = PTR_ERR(env));
2399
2400                                         ref_lli = ll_i2info(ref_inode);
2401                                         ref_obj = ref_lli->lli_clob;
2402                                         cl_object_attr_lock(ref_obj);
2403                                         rc = cl_object_attr_get(env, ref_obj,
2404                                                                 &ref_attr);
2405                                         cl_object_attr_unlock(ref_obj);
2406                                         cl_env_put(env, &refcheck);
2407                                         fput(ref_file);
2408                                         if (rc)
2409                                                 GOTO(out, rc);
2410
2411                                         attr->ia_valid |= ATTR_SIZE;
2412                                         attr->ia_size = ref_attr.cat_size;
2413                                 }
2414                         }
2415                         rc = cl_setattr_ost(lli->lli_clob, attr, xvalid, flags);
2416                 }
2417         }
2418
2419         /* If the file was restored, it needs to set dirty flag.
2420          *
2421          * We've already sent MDS_DATA_MODIFIED flag in
2422          * ll_md_setattr() for truncate. However, the MDT refuses to
2423          * set the HS_DIRTY flag on released files, so we have to set
2424          * it again if the file has been restored. Please check how
2425          * LLIF_DATA_MODIFIED is set in vvp_io_setattr_fini().
2426          *
2427          * Please notice that if the file is not released, the previous
2428          * MDS_DATA_MODIFIED has taken effect and usually
2429          * LLIF_DATA_MODIFIED is not set(see vvp_io_setattr_fini()).
2430          * This way we can save an RPC for common open + trunc
2431          * operation. */
2432         if (test_and_clear_bit(LLIF_DATA_MODIFIED, &lli->lli_flags)) {
2433                 struct hsm_state_set hss = {
2434                         .hss_valid = HSS_SETMASK,
2435                         .hss_setmask = HS_DIRTY,
2436                 };
2437                 int rc2;
2438
2439                 rc2 = ll_hsm_state_set(inode, &hss);
2440                 /* truncate and write can happen at the same time, so that
2441                  * the file can be set modified even though the file is not
2442                  * restored from released state, and ll_hsm_state_set() is
2443                  * not applicable for the file, and rc2 < 0 is normal in this
2444                  * case. */
2445                 if (rc2 < 0)
2446                         CDEBUG(D_INFO, DFID "HSM set dirty failed: rc2 = %d\n",
2447                                PFID(ll_inode2fid(inode)), rc2);
2448         }
2449
2450         EXIT;
2451 out:
2452         if (op_data != NULL)
2453                 ll_finish_md_op_data(op_data);
2454
2455         if (S_ISREG(inode->i_mode)) {
2456                 inode_lock(inode);
2457                 if ((attr->ia_valid & ATTR_SIZE) && !hsm_import)
2458                         inode_dio_wait(inode);
2459                 /* Once we've got the i_mutex, it's safe to set the S_NOSEC
2460                  * flag.  ll_update_inode (called from ll_md_setattr), clears
2461                  * inode flags, so there is a gap where S_NOSEC is not set.
2462                  * This can cause a writer to take the i_mutex unnecessarily,
2463                  * but this is safe to do and should be rare. */
2464                 inode_has_no_xattr(inode);
2465         }
2466
2467         if (!rc)
2468                 ll_stats_ops_tally(ll_i2sbi(inode), attr->ia_valid & ATTR_SIZE ?
2469                                         LPROC_LL_TRUNC : LPROC_LL_SETATTR,
2470                                    ktime_us_delta(ktime_get(), kstart));
2471
2472         RETURN(rc);
2473 }
2474
2475 int ll_setattr(struct user_namespace *mnt_userns, struct dentry *de,
2476                struct iattr *attr)
2477 {
2478         int mode = de->d_inode->i_mode;
2479         enum op_xvalid xvalid = 0;
2480         int rc;
2481
2482         rc = llcrypt_prepare_setattr(de, attr);
2483         if (rc)
2484                 return rc;
2485
2486         if ((attr->ia_valid & (ATTR_CTIME|ATTR_SIZE|ATTR_MODE)) ==
2487                               (ATTR_CTIME|ATTR_SIZE|ATTR_MODE))
2488                 xvalid |= OP_XVALID_OWNEROVERRIDE;
2489
2490         if (((attr->ia_valid & (ATTR_MODE|ATTR_FORCE|ATTR_SIZE)) ==
2491                                (ATTR_SIZE|ATTR_MODE)) &&
2492             (((mode & S_ISUID) && !(attr->ia_mode & S_ISUID)) ||
2493              (((mode & (S_ISGID|S_IXGRP)) == (S_ISGID|S_IXGRP)) &&
2494               !(attr->ia_mode & S_ISGID))))
2495                 attr->ia_valid |= ATTR_FORCE;
2496
2497         if ((attr->ia_valid & ATTR_MODE) &&
2498             (mode & S_ISUID) &&
2499             !(attr->ia_mode & S_ISUID) &&
2500             !(attr->ia_valid & ATTR_KILL_SUID))
2501                 attr->ia_valid |= ATTR_KILL_SUID;
2502
2503         if ((attr->ia_valid & ATTR_MODE) &&
2504             ((mode & (S_ISGID|S_IXGRP)) == (S_ISGID|S_IXGRP)) &&
2505             !(attr->ia_mode & S_ISGID) &&
2506             !(attr->ia_valid & ATTR_KILL_SGID))
2507                 attr->ia_valid |= ATTR_KILL_SGID;
2508
2509         return ll_setattr_raw(de, attr, xvalid, false);
2510 }
2511
2512 int ll_statfs_internal(struct ll_sb_info *sbi, struct obd_statfs *osfs,
2513                        u32 flags)
2514 {
2515         struct obd_statfs obd_osfs = { 0 };
2516         time64_t max_age;
2517         int rc;
2518
2519         ENTRY;
2520         max_age = ktime_get_seconds() - sbi->ll_statfs_max_age;
2521
2522         if (test_bit(LL_SBI_LAZYSTATFS, sbi->ll_flags))
2523                 flags |= OBD_STATFS_NODELAY;
2524
2525         rc = obd_statfs(NULL, sbi->ll_md_exp, osfs, max_age, flags);
2526         if (rc)
2527                 RETURN(rc);
2528
2529         osfs->os_type = LL_SUPER_MAGIC;
2530
2531         CDEBUG(D_SUPER, "MDC blocks %llu/%llu objects %llu/%llu\n",
2532               osfs->os_bavail, osfs->os_blocks, osfs->os_ffree, osfs->os_files);
2533
2534         if (osfs->os_state & OS_STATFS_SUM)
2535                 GOTO(out, rc);
2536
2537         rc = obd_statfs(NULL, sbi->ll_dt_exp, &obd_osfs, max_age, flags);
2538         if (rc) /* Possibly a filesystem with no OSTs.  Report MDT totals. */
2539                 GOTO(out, rc = 0);
2540
2541         CDEBUG(D_SUPER, "OSC blocks %llu/%llu objects %llu/%llu\n",
2542                obd_osfs.os_bavail, obd_osfs.os_blocks, obd_osfs.os_ffree,
2543                obd_osfs.os_files);
2544
2545         osfs->os_bsize = obd_osfs.os_bsize;
2546         osfs->os_blocks = obd_osfs.os_blocks;
2547         osfs->os_bfree = obd_osfs.os_bfree;
2548         osfs->os_bavail = obd_osfs.os_bavail;
2549
2550         /* If we have _some_ OSTs, but don't have as many free objects on the
2551          * OSTs as inodes on the MDTs, reduce the reported number of inodes
2552          * to compensate, so that the "inodes in use" number is correct.
2553          * This should be kept in sync with lod_statfs() behaviour.
2554          */
2555         if (obd_osfs.os_files && obd_osfs.os_ffree < osfs->os_ffree) {
2556                 osfs->os_files = (osfs->os_files - osfs->os_ffree) +
2557                                  obd_osfs.os_ffree;
2558                 osfs->os_ffree = obd_osfs.os_ffree;
2559         }
2560
2561 out:
2562         RETURN(rc);
2563 }
2564
2565 static int ll_statfs_project(struct inode *inode, struct kstatfs *sfs)
2566 {
2567         struct if_quotactl qctl = {
2568                 .qc_cmd = LUSTRE_Q_GETQUOTA,
2569                 .qc_type = PRJQUOTA,
2570                 .qc_valid = QC_GENERAL,
2571         };
2572         u64 limit, curblock;
2573         int ret;
2574
2575         qctl.qc_id = ll_i2info(inode)->lli_projid;
2576         ret = quotactl_ioctl(inode->i_sb, &qctl);
2577         if (ret) {
2578                 /* ignore errors if project ID does not have
2579                  * a quota limit or feature unsupported.
2580                  */
2581                 if (ret == -ESRCH || ret == -EOPNOTSUPP)
2582                         ret = 0;
2583                 return ret;
2584         }
2585
2586         limit = ((qctl.qc_dqblk.dqb_bsoftlimit ?
2587                  qctl.qc_dqblk.dqb_bsoftlimit :
2588                  qctl.qc_dqblk.dqb_bhardlimit) * 1024) / sfs->f_bsize;
2589         if (limit && sfs->f_blocks > limit) {
2590                 curblock = (qctl.qc_dqblk.dqb_curspace +
2591                                 sfs->f_bsize - 1) / sfs->f_bsize;
2592                 sfs->f_blocks = limit;
2593                 sfs->f_bfree = sfs->f_bavail =
2594                         (sfs->f_blocks > curblock) ?
2595                         (sfs->f_blocks - curblock) : 0;
2596         }
2597
2598         limit = qctl.qc_dqblk.dqb_isoftlimit ?
2599                 qctl.qc_dqblk.dqb_isoftlimit :
2600                 qctl.qc_dqblk.dqb_ihardlimit;
2601         if (limit && sfs->f_files > limit) {
2602                 sfs->f_files = limit;
2603                 sfs->f_ffree = (sfs->f_files >
2604                         qctl.qc_dqblk.dqb_curinodes) ?
2605                         (sfs->f_files - qctl.qc_dqblk.dqb_curinodes) : 0;
2606         }
2607
2608         return 0;
2609 }
2610
2611 int ll_statfs(struct dentry *de, struct kstatfs *sfs)
2612 {
2613         struct super_block *sb = de->d_sb;
2614         struct obd_statfs osfs;
2615         __u64 fsid = huge_encode_dev(sb->s_dev);
2616         ktime_t kstart = ktime_get();
2617         int rc;
2618
2619         CDEBUG(D_VFSTRACE, "VFS Op:sb=%s (%p)\n", sb->s_id, sb);
2620
2621         /* Some amount of caching on the client is allowed */
2622         rc = ll_statfs_internal(ll_s2sbi(sb), &osfs, OBD_STATFS_SUM);
2623         if (rc)
2624                 return rc;
2625
2626         statfs_unpack(sfs, &osfs);
2627
2628         /* We need to downshift for all 32-bit kernels, because we can't
2629          * tell if the kernel is being called via sys_statfs64() or not.
2630          * Stop before overflowing f_bsize - in which case it is better
2631          * to just risk EOVERFLOW if caller is using old sys_statfs(). */
2632         if (sizeof(long) < 8) {
2633                 while (osfs.os_blocks > ~0UL && sfs->f_bsize < 0x40000000) {
2634                         sfs->f_bsize <<= 1;
2635
2636                         osfs.os_blocks >>= 1;
2637                         osfs.os_bfree >>= 1;
2638                         osfs.os_bavail >>= 1;
2639                 }
2640         }
2641
2642         sfs->f_blocks = osfs.os_blocks;
2643         sfs->f_bfree = osfs.os_bfree;
2644         sfs->f_bavail = osfs.os_bavail;
2645         sfs->f_fsid.val[0] = (__u32)fsid;
2646         sfs->f_fsid.val[1] = (__u32)(fsid >> 32);
2647         if (ll_i2info(de->d_inode)->lli_projid &&
2648             test_bit(LLIF_PROJECT_INHERIT, &ll_i2info(de->d_inode)->lli_flags))
2649                 return ll_statfs_project(de->d_inode, sfs);
2650
2651         ll_stats_ops_tally(ll_s2sbi(sb), LPROC_LL_STATFS,
2652                            ktime_us_delta(ktime_get(), kstart));
2653
2654         return 0;
2655 }
2656
2657 void ll_inode_size_lock(struct inode *inode)
2658 {
2659         struct ll_inode_info *lli;
2660
2661         LASSERT(!S_ISDIR(inode->i_mode));
2662
2663         lli = ll_i2info(inode);
2664         mutex_lock(&lli->lli_size_mutex);
2665 }
2666
2667 void ll_inode_size_unlock(struct inode *inode)
2668 {
2669         struct ll_inode_info *lli;
2670
2671         lli = ll_i2info(inode);
2672         mutex_unlock(&lli->lli_size_mutex);
2673 }
2674
2675 int ll_inode_size_trylock(struct inode *inode)
2676 {
2677         struct ll_inode_info *lli;
2678
2679         LASSERT(!S_ISDIR(inode->i_mode));
2680
2681         lli = ll_i2info(inode);
2682         return mutex_trylock(&lli->lli_size_mutex);
2683 }
2684
2685 void ll_update_inode_flags(struct inode *inode, unsigned int ext_flags)
2686 {
2687         /* do not clear encryption flag */
2688         ext_flags |= ll_inode_to_ext_flags(inode->i_flags) & LUSTRE_ENCRYPT_FL;
2689         inode->i_flags = ll_ext_to_inode_flags(ext_flags);
2690         if (ext_flags & LUSTRE_PROJINHERIT_FL)
2691                 set_bit(LLIF_PROJECT_INHERIT, &ll_i2info(inode)->lli_flags);
2692         else
2693                 clear_bit(LLIF_PROJECT_INHERIT, &ll_i2info(inode)->lli_flags);
2694 }
2695
2696 int ll_update_inode(struct inode *inode, struct lustre_md *md)
2697 {
2698         struct ll_inode_info *lli = ll_i2info(inode);
2699         struct mdt_body *body = md->body;
2700         struct ll_sb_info *sbi = ll_i2sbi(inode);
2701         bool api32;
2702         int rc = 0;
2703
2704         if (body->mbo_valid & OBD_MD_FLEASIZE) {
2705                 rc = cl_file_inode_init(inode, md);
2706                 if (rc)
2707                         return rc;
2708         }
2709
2710         if (S_ISDIR(inode->i_mode)) {
2711                 rc = ll_update_lsm_md(inode, md);
2712                 if (rc != 0)
2713                         return rc;
2714         }
2715
2716         if (body->mbo_valid & OBD_MD_FLACL)
2717                 lli_replace_acl(lli, md);
2718
2719         api32 = test_bit(LL_SBI_32BIT_API, sbi->ll_flags);
2720         inode->i_ino = cl_fid_build_ino(&body->mbo_fid1, api32);
2721         inode->i_generation = cl_fid_build_gen(&body->mbo_fid1);
2722
2723         if (body->mbo_valid & OBD_MD_FLATIME) {
2724                 if (body->mbo_atime > inode->i_atime.tv_sec)
2725                         inode->i_atime.tv_sec = body->mbo_atime;
2726                 lli->lli_atime = body->mbo_atime;
2727         }
2728
2729         if (body->mbo_valid & OBD_MD_FLMTIME) {
2730                 if (body->mbo_mtime > inode->i_mtime.tv_sec) {
2731                         CDEBUG(D_INODE,
2732                                "setting ino %lu mtime from %lld to %llu\n",
2733                                inode->i_ino, (s64)inode->i_mtime.tv_sec,
2734                                body->mbo_mtime);
2735                         inode->i_mtime.tv_sec = body->mbo_mtime;
2736                 }
2737                 lli->lli_mtime = body->mbo_mtime;
2738         }
2739
2740         if (body->mbo_valid & OBD_MD_FLCTIME) {
2741                 if (body->mbo_ctime > inode->i_ctime.tv_sec)
2742                         inode->i_ctime.tv_sec = body->mbo_ctime;
2743                 lli->lli_ctime = body->mbo_ctime;
2744         }
2745
2746         if (body->mbo_valid & OBD_MD_FLBTIME)
2747                 lli->lli_btime = body->mbo_btime;
2748
2749         /* Clear i_flags to remove S_NOSEC before permissions are updated */
2750         if (body->mbo_valid & OBD_MD_FLFLAGS)
2751                 ll_update_inode_flags(inode, body->mbo_flags);
2752         if (body->mbo_valid & OBD_MD_FLMODE)
2753                 inode->i_mode = (inode->i_mode & S_IFMT) |
2754                                 (body->mbo_mode & ~S_IFMT);
2755
2756         if (body->mbo_valid & OBD_MD_FLTYPE)
2757                 inode->i_mode = (inode->i_mode & ~S_IFMT) |
2758                                 (body->mbo_mode & S_IFMT);
2759
2760         LASSERT(inode->i_mode != 0);
2761         if (body->mbo_valid & OBD_MD_FLUID)
2762                 inode->i_uid = make_kuid(&init_user_ns, body->mbo_uid);
2763         if (body->mbo_valid & OBD_MD_FLGID)
2764                 inode->i_gid = make_kgid(&init_user_ns, body->mbo_gid);
2765         if (body->mbo_valid & OBD_MD_FLPROJID)
2766                 lli->lli_projid = body->mbo_projid;
2767         if (body->mbo_valid & OBD_MD_FLNLINK) {
2768                 spin_lock(&inode->i_lock);
2769                 set_nlink(inode, body->mbo_nlink);
2770                 spin_unlock(&inode->i_lock);
2771         }
2772         if (body->mbo_valid & OBD_MD_FLRDEV)
2773                 inode->i_rdev = old_decode_dev(body->mbo_rdev);
2774
2775         if (body->mbo_valid & OBD_MD_FLID) {
2776                 /* FID shouldn't be changed! */
2777                 if (fid_is_sane(&lli->lli_fid)) {
2778                         LASSERTF(lu_fid_eq(&lli->lli_fid, &body->mbo_fid1),
2779                                  "Trying to change FID "DFID
2780                                  " to the "DFID", inode "DFID"(%p)\n",
2781                                  PFID(&lli->lli_fid), PFID(&body->mbo_fid1),
2782                                  PFID(ll_inode2fid(inode)), inode);
2783                 } else {
2784                         lli->lli_fid = body->mbo_fid1;
2785                 }
2786         }
2787
2788         LASSERT(fid_seq(&lli->lli_fid) != 0);
2789
2790         /* In case of encrypted file without the key, please do not lose
2791          * clear text size stored into lli_lazysize in ll_merge_attr(),
2792          * we will need it in ll_prepare_close().
2793          */
2794         if (lli->lli_attr_valid & OBD_MD_FLLAZYSIZE && lli->lli_lazysize &&
2795             llcrypt_require_key(inode) == -ENOKEY)
2796                 lli->lli_attr_valid = body->mbo_valid | OBD_MD_FLLAZYSIZE;
2797         else
2798                 lli->lli_attr_valid = body->mbo_valid;
2799         if (body->mbo_valid & OBD_MD_FLSIZE) {
2800                 i_size_write(inode, body->mbo_size);
2801
2802                 CDEBUG(D_VFSTRACE, "inode="DFID", updating i_size %llu\n",
2803                        PFID(ll_inode2fid(inode)),
2804                        (unsigned long long)body->mbo_size);
2805
2806                 if (body->mbo_valid & OBD_MD_FLBLOCKS)
2807                         inode->i_blocks = body->mbo_blocks;
2808         } else {
2809                 if (body->mbo_valid & OBD_MD_FLLAZYSIZE)
2810                         lli->lli_lazysize = body->mbo_size;
2811                 if (body->mbo_valid & OBD_MD_FLLAZYBLOCKS)
2812                         lli->lli_lazyblocks = body->mbo_blocks;
2813         }
2814
2815         if (body->mbo_valid & OBD_MD_TSTATE) {
2816                 /* Set LLIF_FILE_RESTORING if restore ongoing and
2817                  * clear it when done to ensure to start again
2818                  * glimpsing updated attrs
2819                  */
2820                 if (body->mbo_t_state & MS_RESTORE)
2821                         set_bit(LLIF_FILE_RESTORING, &lli->lli_flags);
2822                 else
2823                         clear_bit(LLIF_FILE_RESTORING, &lli->lli_flags);
2824         }
2825
2826         return 0;
2827 }
2828
2829 /* child default LMV is inherited from parent */
2830 static inline bool ll_default_lmv_inherited(struct lmv_stripe_md *pdmv,
2831                                             struct lmv_stripe_md *cdmv)
2832 {
2833         if (!pdmv || !cdmv)
2834                 return false;
2835
2836         if (pdmv->lsm_md_magic != cdmv->lsm_md_magic ||
2837             pdmv->lsm_md_stripe_count != cdmv->lsm_md_stripe_count ||
2838             pdmv->lsm_md_master_mdt_index != cdmv->lsm_md_master_mdt_index ||
2839             pdmv->lsm_md_hash_type != cdmv->lsm_md_hash_type)
2840                 return false;
2841
2842         if (cdmv->lsm_md_max_inherit !=
2843             lmv_inherit_next(pdmv->lsm_md_max_inherit))
2844                 return false;
2845
2846         if (cdmv->lsm_md_max_inherit_rr !=
2847             lmv_inherit_rr_next(pdmv->lsm_md_max_inherit_rr))
2848                 return false;
2849
2850         return true;
2851 }
2852
2853 /* if default LMV is implicitly inherited, subdir default LMV is maintained on
2854  * client side.
2855  */
2856 int ll_dir_default_lmv_inherit(struct inode *dir, struct inode *inode)
2857 {
2858         struct ll_inode_info *plli = ll_i2info(dir);
2859         struct ll_inode_info *lli = ll_i2info(inode);
2860         struct lmv_stripe_md *plsm;
2861         struct lmv_stripe_md *lsm;
2862         int rc = 0;
2863
2864         ENTRY;
2865
2866         /* ROOT default LMV is not inherited */
2867         if (is_root_inode(dir) ||
2868             !(exp_connect_flags2(ll_i2mdexp(dir)) &
2869                                  OBD_CONNECT2_DMV_IMP_INHERIT))
2870                 RETURN(0);
2871
2872         /* nothing to do if no default LMV on both */
2873         if (!plli->lli_default_lsm_md && !lli->lli_default_lsm_md)
2874                 RETURN(0);
2875
2876         /* subdir default LMV comes from disk */
2877         if (lli->lli_default_lsm_md && lli->lli_default_lmv_set)
2878                 RETURN(0);
2879
2880         /* delete subdir default LMV if parent's is deleted or becomes
2881          * uninheritable.
2882          */
2883         down_read(&plli->lli_lsm_sem);
2884         plsm = plli->lli_default_lsm_md;
2885         if (!plsm || !lmv_is_inheritable(plsm->lsm_md_max_inherit)) {
2886                 if (lli->lli_default_lsm_md && !lli->lli_default_lmv_set) {
2887                         down_write(&lli->lli_lsm_sem);
2888                         if (lli->lli_default_lsm_md &&
2889                             !lli->lli_default_lmv_set) {
2890                                 lmv_free_memmd(lli->lli_default_lsm_md);
2891                                 lli->lli_default_lsm_md = NULL;
2892                                 lli->lli_inherit_depth = 0;
2893                         }
2894                         up_write(&lli->lli_lsm_sem);
2895                 }
2896                 GOTO(unlock_parent, rc = 0);
2897         }
2898
2899         /* do nothing if inherited LMV is unchanged */
2900         if (lli->lli_default_lsm_md) {
2901                 rc = 1;
2902                 down_read(&lli->lli_lsm_sem);
2903                 if (!lli->lli_default_lmv_set)
2904                         rc = lsm_md_inherited(plsm, lli->lli_default_lsm_md);
2905                 up_read(&lli->lli_lsm_sem);
2906                 if (rc == 1)
2907                         GOTO(unlock_parent, rc = 0);
2908         }
2909
2910         /* inherit default LMV */
2911         down_write(&lli->lli_lsm_sem);
2912         if (lli->lli_default_lsm_md) {
2913                 /* checked above, but in case of race, check again with lock */
2914                 if (lli->lli_default_lmv_set)
2915                         GOTO(unlock_child, rc = 0);
2916                 /* always update subdir default LMV in case parent's changed */
2917                 lsm = lli->lli_default_lsm_md;
2918         } else {
2919                 OBD_ALLOC_PTR(lsm);
2920                 if (!lsm)
2921                         GOTO(unlock_child, rc = -ENOMEM);
2922                 lli->lli_default_lsm_md = lsm;
2923         }
2924
2925         *lsm = *plsm;
2926         lsm->lsm_md_max_inherit = lmv_inherit_next(plsm->lsm_md_max_inherit);
2927         lsm->lsm_md_max_inherit_rr =
2928                         lmv_inherit_rr_next(plsm->lsm_md_max_inherit_rr);
2929         lli->lli_inherit_depth = plli->lli_inherit_depth + 1;
2930
2931         lsm_md_dump(D_INODE, lsm);
2932
2933         EXIT;
2934 unlock_child:
2935         up_write(&lli->lli_lsm_sem);
2936 unlock_parent:
2937         up_read(&plli->lli_lsm_sem);
2938
2939         return rc;
2940 }
2941
2942 /**
2943  * Update directory depth and default LMV
2944  *
2945  * Update directory depth to ROOT and inherit default LMV from parent if
2946  * parent's default LMV is inheritable. The default LMV set with command
2947  * "lfs setdirstripe -D ..." is stored on MDT, while the inherited default LMV
2948  * is generated at runtime on client side.
2949  *
2950  * \param[in]   dir     parent directory inode
2951  * \param[in]   de      dentry
2952  */
2953 void ll_update_dir_depth_dmv(struct inode *dir, struct dentry *de)
2954 {
2955         struct inode *inode = de->d_inode;
2956         struct ll_inode_info *plli;
2957         struct ll_inode_info *lli;
2958
2959         LASSERT(S_ISDIR(inode->i_mode));
2960         if (inode == dir)
2961                 return;
2962
2963         plli = ll_i2info(dir);
2964         lli = ll_i2info(inode);
2965         lli->lli_dir_depth = plli->lli_dir_depth + 1;
2966         if (lli->lli_default_lsm_md && lli->lli_default_lmv_set) {
2967                 if (plli->lli_default_lsm_md) {
2968                         down_read(&plli->lli_lsm_sem);
2969                         down_read(&lli->lli_lsm_sem);
2970                         if (lsm_md_inherited(plli->lli_default_lsm_md,
2971                                              lli->lli_default_lsm_md))
2972                                 lli->lli_inherit_depth =
2973                                         plli->lli_inherit_depth + 1;
2974                         else
2975                                 /* in case parent default LMV changed */
2976                                 lli->lli_inherit_depth = 0;
2977                         up_read(&lli->lli_lsm_sem);
2978                         up_read(&plli->lli_lsm_sem);
2979                 } else {
2980                         /* in case parent default LMV deleted */
2981                         lli->lli_inherit_depth = 0;
2982                 }
2983         } else {
2984                 ll_dir_default_lmv_inherit(dir, inode);
2985         }
2986
2987         if (lli->lli_default_lsm_md)
2988                 CDEBUG(D_INODE,
2989                        "%s "DFID" depth %hu %s default LMV inherit depth %hu\n",
2990                        de->d_name.name, PFID(&lli->lli_fid), lli->lli_dir_depth,
2991                        lli->lli_default_lmv_set ? "server" : "client",
2992                        lli->lli_inherit_depth);
2993 }
2994
2995 void ll_truncate_inode_pages_final(struct inode *inode, struct cl_io *io)
2996 {
2997         struct address_space *mapping = &inode->i_data;
2998         unsigned long nrpages;
2999         unsigned long flags;
3000
3001         LASSERTF(io == NULL || inode_is_locked(inode), "io %p (type %d)\n",
3002                  io, io ? io->ci_type : 0);
3003
3004         truncate_inode_pages_final(mapping);
3005
3006         /* Workaround for LU-118: Note nrpages may not be totally updated when
3007          * truncate_inode_pages() returns, as there can be a page in the process
3008          * of deletion (inside __delete_from_page_cache()) in the specified
3009          * range. Thus mapping->nrpages can be non-zero when this function
3010          * returns even after truncation of the whole mapping.  Only do this if
3011          * npages isn't already zero.
3012          */
3013         nrpages = mapping->nrpages;
3014         if (nrpages) {
3015                 ll_xa_lock_irqsave(&mapping->i_pages, flags);
3016                 nrpages = mapping->nrpages;
3017                 ll_xa_unlock_irqrestore(&mapping->i_pages, flags);
3018         } /* Workaround end */
3019
3020         LASSERTF(nrpages == 0, "%s: inode="DFID"(%p) nrpages=%lu "
3021                  "io %p (io_type %d), "
3022                  "see https://jira.whamcloud.com/browse/LU-118\n",
3023                  ll_i2sbi(inode)->ll_fsname,
3024                  PFID(ll_inode2fid(inode)), inode, nrpages,
3025                  io, io ? io->ci_type : 0);
3026 }
3027
3028 int ll_read_inode2(struct inode *inode, void *opaque)
3029 {
3030         struct lustre_md *md = opaque;
3031         struct ll_inode_info *lli = ll_i2info(inode);
3032         int     rc;
3033         ENTRY;
3034
3035         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
3036                PFID(&lli->lli_fid), inode);
3037
3038         /* Core attributes from the MDS first.  This is a new inode, and
3039          * the VFS doesn't zero times in the core inode so we have to do
3040          * it ourselves.  They will be overwritten by either MDS or OST
3041          * attributes - we just need to make sure they aren't newer.
3042          */
3043         inode->i_mtime.tv_sec = 0;
3044         inode->i_atime.tv_sec = 0;
3045         inode->i_ctime.tv_sec = 0;
3046         inode->i_rdev = 0;
3047         rc = ll_update_inode(inode, md);
3048         if (rc != 0)
3049                 RETURN(rc);
3050
3051         /* OIDEBUG(inode); */
3052
3053 #ifdef HAVE_BACKING_DEV_INFO
3054         /* initializing backing dev info. */
3055         inode->i_mapping->backing_dev_info = &s2lsi(inode->i_sb)->lsi_bdi;
3056 #endif
3057         if (S_ISREG(inode->i_mode)) {
3058                 struct ll_sb_info *sbi = ll_i2sbi(inode);
3059                 inode->i_op = &ll_file_inode_operations;
3060                 inode->i_fop = sbi->ll_fop;
3061                 inode->i_mapping->a_ops = (struct address_space_operations *)&ll_aops;
3062                 EXIT;
3063         } else if (S_ISDIR(inode->i_mode)) {
3064                 inode->i_op = &ll_dir_inode_operations;
3065                 inode->i_fop = &ll_dir_operations;
3066                 EXIT;
3067         } else if (S_ISLNK(inode->i_mode)) {
3068                 inode->i_op = &ll_fast_symlink_inode_operations;
3069                 EXIT;
3070         } else {
3071                 inode->i_op = &ll_special_inode_operations;
3072
3073                 init_special_inode(inode, inode->i_mode,
3074                                    inode->i_rdev);
3075
3076                 EXIT;
3077         }
3078
3079         return 0;
3080 }
3081
3082 void ll_delete_inode(struct inode *inode)
3083 {
3084         struct ll_inode_info *lli = ll_i2info(inode);
3085         ENTRY;
3086
3087         if (S_ISREG(inode->i_mode) && lli->lli_clob != NULL) {
3088                 /* It is last chance to write out dirty pages,
3089                  * otherwise we may lose data while umount.
3090                  *
3091                  * If i_nlink is 0 then just discard data. This is safe because
3092                  * local inode gets i_nlink 0 from server only for the last
3093                  * unlink, so that file is not opened somewhere else
3094                  */
3095                 cl_sync_file_range(inode, 0, OBD_OBJECT_EOF, inode->i_nlink ?
3096                                    CL_FSYNC_LOCAL : CL_FSYNC_DISCARD, 1);
3097         }
3098
3099         ll_truncate_inode_pages_final(inode, NULL);
3100         ll_clear_inode(inode);
3101         clear_inode(inode);
3102
3103         EXIT;
3104 }
3105
3106 /* ioctl commands shared between files and directories */
3107 int ll_iocontrol(struct inode *inode, struct file *file,
3108                  unsigned int cmd, void __user *uarg)
3109 {
3110         struct ll_sb_info *sbi = ll_i2sbi(inode);
3111         struct ptlrpc_request *req = NULL;
3112         int rc, flags = 0;
3113         ENTRY;
3114
3115         switch (cmd) {
3116         case BLKSSZGET:
3117                 RETURN(put_user(PAGE_SIZE, (int __user *)uarg));
3118         case LL_IOC_GETVERSION:
3119         case FS_IOC_GETVERSION:
3120                 RETURN(put_user(inode->i_generation, (int __user *)uarg));
3121         case FS_IOC_GETFLAGS: {
3122                 struct mdt_body *body;
3123                 struct md_op_data *op_data;
3124
3125                 if (!ll_access_ok(uarg, sizeof(int)))
3126                         RETURN(-EFAULT);
3127
3128                 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
3129                                              LUSTRE_OPC_ANY, NULL);
3130                 if (IS_ERR(op_data))
3131                         RETURN(PTR_ERR(op_data));
3132
3133                 op_data->op_valid = OBD_MD_FLFLAGS;
3134                 rc = md_getattr(sbi->ll_md_exp, op_data, &req);
3135                 ll_finish_md_op_data(op_data);
3136                 if (rc) {
3137                         CERROR("%s: failure inode "DFID": rc = %d\n",
3138                                sbi->ll_md_exp->exp_obd->obd_name,
3139                                PFID(ll_inode2fid(inode)), rc);
3140                         RETURN(-abs(rc));
3141                 }
3142
3143                 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
3144
3145                 flags = body->mbo_flags;
3146                 /* if Lustre specific LUSTRE_ENCRYPT_FL flag is set, also set
3147                  * ext4 equivalent to please lsattr and other e2fsprogs tools
3148                  */
3149                 if (flags & LUSTRE_ENCRYPT_FL)
3150                         flags |= STATX_ATTR_ENCRYPTED;
3151
3152                 ptlrpc_req_finished(req);
3153
3154                 RETURN(put_user(flags, (int __user *)uarg));
3155         }
3156         case FS_IOC_SETFLAGS: {
3157                 struct iattr *attr;
3158                 struct md_op_data *op_data;
3159                 struct cl_object *obj;
3160                 struct fsxattr fa = { 0 };
3161
3162                 if (get_user(flags, (int __user *)uarg))
3163                         RETURN(-EFAULT);
3164
3165                 fa.fsx_projid = ll_i2info(inode)->lli_projid;
3166                 if (flags & LUSTRE_PROJINHERIT_FL)
3167                         fa.fsx_xflags = FS_XFLAG_PROJINHERIT;
3168
3169                 rc = ll_ioctl_check_project(inode, fa.fsx_xflags,
3170                                             fa.fsx_projid);
3171                 if (rc)
3172                         RETURN(rc);
3173
3174                 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
3175                                              LUSTRE_OPC_ANY, NULL);
3176                 if (IS_ERR(op_data))
3177                         RETURN(PTR_ERR(op_data));
3178
3179                 op_data->op_attr_flags = flags;
3180                 op_data->op_xvalid |= OP_XVALID_FLAGS;
3181                 rc = md_setattr(sbi->ll_md_exp, op_data, NULL, 0, &req);
3182                 ll_finish_md_op_data(op_data);
3183                 ptlrpc_req_finished(req);
3184                 if (rc)
3185                         RETURN(rc);
3186
3187                 ll_update_inode_flags(inode, flags);
3188
3189                 obj = ll_i2info(inode)->lli_clob;
3190                 if (obj == NULL)
3191                         RETURN(0);
3192
3193                 OBD_ALLOC_PTR(attr);
3194                 if (attr == NULL)
3195                         RETURN(-ENOMEM);
3196
3197                 rc = cl_setattr_ost(obj, attr, OP_XVALID_FLAGS, flags);
3198
3199                 OBD_FREE_PTR(attr);
3200                 RETURN(rc);
3201         }
3202         case FS_IOC_FSGETXATTR:
3203                 RETURN(ll_ioctl_fsgetxattr(inode, cmd, uarg));
3204         case FS_IOC_FSSETXATTR:
3205                 RETURN(ll_ioctl_fssetxattr(inode, cmd, uarg));
3206         case LL_IOC_PROJECT:
3207                 RETURN(ll_ioctl_project(file, cmd, uarg));
3208         case IOC_OBD_STATFS:
3209                 RETURN(ll_obd_statfs(inode, uarg));
3210         case LL_IOC_GET_MDTIDX: {
3211                 if (!ll_access_ok(uarg, sizeof(rc)))
3212                         RETURN(-EFAULT);
3213
3214                 rc = ll_get_mdt_idx(inode);
3215                 if (rc < 0)
3216                         RETURN(rc);
3217
3218                 if (put_user(rc, (int __user *)uarg))
3219                         RETURN(-EFAULT);
3220
3221                 RETURN(0);
3222         }
3223         case LL_IOC_FLUSHCTX:
3224                 RETURN(ll_flush_ctx(inode));
3225 #ifdef HAVE_LUSTRE_CRYPTO
3226         case LL_IOC_ADD_ENCRYPTION_KEY:
3227                 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
3228                         return -EOPNOTSUPP;
3229                 rc = llcrypt_ioctl_add_key(file, uarg);
3230 #ifdef CONFIG_LL_ENCRYPTION
3231                 if (!rc && S_ISDIR(inode->i_mode))
3232                         sptlrpc_enc_pool_add_user();
3233 #endif
3234                 RETURN(rc);
3235         case LL_IOC_GET_ENCRYPTION_KEY_STATUS:
3236                 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
3237                         return -EOPNOTSUPP;
3238                 RETURN(llcrypt_ioctl_get_key_status(file, uarg));
3239         case LL_IOC_GET_ENCRYPTION_POLICY_EX:
3240                 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
3241                         return -EOPNOTSUPP;
3242                 RETURN(llcrypt_ioctl_get_policy_ex(file, uarg));
3243         case LL_IOC_SET_ENCRYPTION_POLICY:
3244                 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
3245                         return -EOPNOTSUPP;
3246                 RETURN(llcrypt_ioctl_set_policy(file, uarg));
3247         case LL_IOC_REMOVE_ENCRYPTION_KEY:
3248                 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
3249                         return -EOPNOTSUPP;
3250                 RETURN(llcrypt_ioctl_remove_key(file, uarg));
3251         case LL_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
3252                 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
3253                         return -EOPNOTSUPP;
3254                 RETURN(llcrypt_ioctl_remove_key_all_users(file, uarg));
3255 #endif
3256         case LL_IOC_GETPARENT:
3257                 RETURN(ll_getparent(file, uarg));
3258         case LL_IOC_PATH2FID:
3259                 if (copy_to_user(uarg, ll_inode2fid(inode),
3260                                  sizeof(struct lu_fid)))
3261                         RETURN(-EFAULT);
3262                 RETURN(0);
3263         case LL_IOC_UNLOCK_FOREIGN: {
3264                 struct dentry *dentry = file_dentry(file);
3265
3266                 /* if not a foreign symlink do nothing */
3267                 if (ll_foreign_is_removable(dentry, true)) {
3268                         CDEBUG(D_INFO,
3269                                "prevent unlink of non-foreign file ("DFID")\n",
3270                                PFID(ll_inode2fid(inode)));
3271                         RETURN(-EOPNOTSUPP);
3272                 }
3273                 RETURN(0);
3274         }
3275         case OBD_IOC_FID2PATH:
3276                 RETURN(ll_fid2path(inode, uarg));
3277 #ifdef OBD_IOC_GETNAME_OLD
3278         case_OBD_IOC_DEPRECATED_FT(OBD_IOC_GETNAME_OLD,
3279                                    sbi->ll_md_exp->exp_obd->obd_name, 2, 16);
3280 #endif
3281         case OBD_IOC_GETDTNAME:
3282         case OBD_IOC_GETMDNAME:
3283                 RETURN(ll_get_obd_name(inode, cmd, uarg));
3284         default:
3285                 RETURN(-ENOTTY);
3286         }
3287
3288         RETURN(0);
3289 }
3290
3291 int ll_flush_ctx(struct inode *inode)
3292 {
3293         struct ll_sb_info  *sbi = ll_i2sbi(inode);
3294
3295         CDEBUG(D_SEC, "flush context for user %d\n",
3296                from_kuid(&init_user_ns, current_uid()));
3297
3298         obd_set_info_async(NULL, sbi->ll_md_exp,
3299                            sizeof(KEY_FLUSH_CTX), KEY_FLUSH_CTX,
3300                            0, NULL, NULL);
3301         obd_set_info_async(NULL, sbi->ll_dt_exp,
3302                            sizeof(KEY_FLUSH_CTX), KEY_FLUSH_CTX,
3303                            0, NULL, NULL);
3304         return 0;
3305 }
3306
3307 /* umount -f client means force down, don't save state */
3308 void ll_umount_begin(struct super_block *sb)
3309 {
3310         struct ll_sb_info *sbi = ll_s2sbi(sb);
3311         struct obd_device *obd;
3312         struct obd_ioctl_data *ioc_data;
3313         int cnt;
3314         ENTRY;
3315
3316         CDEBUG(D_VFSTRACE, "VFS Op: superblock %p count %d active %d\n", sb,
3317                sb->s_count, atomic_read(&sb->s_active));
3318
3319         obd = class_exp2obd(sbi->ll_md_exp);
3320         if (obd == NULL) {
3321                 CERROR("Invalid MDC connection handle %#llx\n",
3322                        sbi->ll_md_exp->exp_handle.h_cookie);
3323                 EXIT;
3324                 return;
3325         }
3326         obd->obd_force = 1;
3327
3328         obd = class_exp2obd(sbi->ll_dt_exp);
3329         if (obd == NULL) {
3330                 CERROR("Invalid LOV connection handle %#llx\n",
3331                        sbi->ll_dt_exp->exp_handle.h_cookie);
3332                 EXIT;
3333                 return;
3334         }
3335         obd->obd_force = 1;
3336
3337         OBD_ALLOC_PTR(ioc_data);
3338         if (ioc_data) {
3339                 obd_iocontrol(OBD_IOC_SET_ACTIVE, sbi->ll_md_exp,
3340                               sizeof *ioc_data, ioc_data, NULL);
3341
3342                 obd_iocontrol(OBD_IOC_SET_ACTIVE, sbi->ll_dt_exp,
3343                               sizeof *ioc_data, ioc_data, NULL);
3344
3345                 OBD_FREE_PTR(ioc_data);
3346         }
3347
3348         /* Really, we'd like to wait until there are no requests outstanding,
3349          * and then continue.  For now, we just periodically checking for vfs
3350          * to decrement mnt_cnt and hope to finish it within 10sec.
3351          */
3352         cnt = 10;
3353         while (cnt > 0 &&
3354                !may_umount(sbi->ll_mnt.mnt)) {
3355                 ssleep(1);
3356                 cnt -= 1;
3357         }
3358
3359         EXIT;
3360 }
3361
3362 int ll_remount_fs(struct super_block *sb, int *flags, char *data)
3363 {
3364         struct ll_sb_info *sbi = ll_s2sbi(sb);
3365         char *profilenm = get_profile_name(sb);
3366         int err;
3367         __u32 read_only;
3368
3369         if ((*flags & MS_RDONLY) != (sb->s_flags & SB_RDONLY)) {
3370                 read_only = *flags & MS_RDONLY;
3371                 err = obd_set_info_async(NULL, sbi->ll_md_exp,
3372                                          sizeof(KEY_READ_ONLY),
3373                                          KEY_READ_ONLY, sizeof(read_only),
3374                                          &read_only, NULL);
3375                 if (err) {
3376                         LCONSOLE_WARN("Failed to remount %s %s (%d)\n",
3377                                       profilenm, read_only ?
3378                                       "read-only" : "read-write", err);
3379                         return err;
3380                 }
3381
3382                 if (read_only)
3383                         sb->s_flags |= SB_RDONLY;
3384                 else
3385                         sb->s_flags &= ~SB_RDONLY;
3386
3387                 if (test_bit(LL_SBI_VERBOSE, sbi->ll_flags))
3388                         LCONSOLE_WARN("Remounted %s %s\n", profilenm,
3389                                       read_only ?  "read-only" : "read-write");
3390         }
3391         return 0;
3392 }
3393
3394 /**
3395  * Cleanup the open handle that is cached on MDT-side.
3396  *
3397  * For open case, the client side open handling thread may hit error
3398  * after the MDT grant the open. Under such case, the client should
3399  * send close RPC to the MDT as cleanup; otherwise, the open handle
3400  * on the MDT will be leaked there until the client umount or evicted.
3401  *
3402  * In further, if someone unlinked the file, because the open handle
3403  * holds the reference on such file/object, then it will block the
3404  * subsequent threads that want to locate such object via FID.
3405  *
3406  * \param[in] sb        super block for this file-system
3407  * \param[in] open_req  pointer to the original open request
3408  */
3409 void ll_open_cleanup(struct super_block *sb, struct req_capsule *pill)
3410 {
3411         struct mdt_body                 *body;
3412         struct md_op_data               *op_data;
3413         struct ptlrpc_request           *close_req = NULL;
3414         struct obd_export               *exp       = ll_s2sbi(sb)->ll_md_exp;
3415         ENTRY;
3416
3417         body = req_capsule_server_get(pill, &RMF_MDT_BODY);
3418         OBD_ALLOC_PTR(op_data);
3419         if (op_data == NULL) {
3420                 CWARN("%s: cannot allocate op_data to release open handle for "
3421                       DFID"\n", ll_s2sbi(sb)->ll_fsname, PFID(&body->mbo_fid1));
3422
3423                 RETURN_EXIT;
3424         }
3425
3426         op_data->op_fid1 = body->mbo_fid1;
3427         op_data->op_open_handle = body->mbo_open_handle;
3428         op_data->op_mod_time = ktime_get_real_seconds();
3429         md_close(exp, op_data, NULL, &close_req);
3430         ptlrpc_req_finished(close_req);
3431         ll_finish_md_op_data(op_data);
3432
3433         EXIT;
3434 }
3435
3436 /* set filesystem-wide default LMV for subdir mount if it's enabled on ROOT. */
3437 static int ll_fileset_default_lmv_fixup(struct inode *inode,
3438                                         struct lustre_md *md)
3439 {
3440         struct ll_sb_info *sbi = ll_i2sbi(inode);
3441         struct ptlrpc_request *req = NULL;
3442         union lmv_mds_md *lmm = NULL;
3443         int size = 0;
3444         int rc;
3445
3446         LASSERT(is_root_inode(inode));
3447         LASSERT(!fid_is_root(&sbi->ll_root_fid));
3448         LASSERT(!md->default_lmv);
3449
3450         rc = ll_dir_get_default_layout(inode, (void **)&lmm, &size, &req,
3451                                        OBD_MD_DEFAULT_MEA,
3452                                        GET_DEFAULT_LAYOUT_ROOT);
3453         if (rc && rc != -ENODATA)
3454                 GOTO(out, rc);
3455
3456         rc = 0;
3457         if (lmm && size) {
3458                 rc = md_unpackmd(sbi->ll_md_exp, &md->default_lmv, lmm, size);
3459                 if (rc < 0)
3460                         GOTO(out, rc);
3461
3462                 rc = 0;
3463         }
3464         EXIT;
3465 out:
3466         if (req)
3467                 ptlrpc_req_finished(req);
3468         return rc;
3469 }
3470
3471 int ll_prep_inode(struct inode **inode, struct req_capsule *pill,
3472                   struct super_block *sb, struct lookup_intent *it)
3473 {
3474         struct ll_sb_info *sbi = NULL;
3475         struct lustre_md md = { NULL };
3476         bool default_lmv_deleted = false;
3477         int rc;
3478
3479         ENTRY;
3480
3481         LASSERT(*inode || sb);
3482         sbi = sb ? ll_s2sbi(sb) : ll_i2sbi(*inode);
3483         rc = md_get_lustre_md(sbi->ll_md_exp, pill, sbi->ll_dt_exp,
3484                               sbi->ll_md_exp, &md);
3485         if (rc != 0)
3486                 GOTO(out, rc);
3487
3488         /*
3489          * clear default_lmv only if intent_getattr reply doesn't contain it.
3490          * but it needs to be done after iget, check this early because
3491          * ll_update_lsm_md() may change md.
3492          */
3493         if (it && (it->it_op & (IT_LOOKUP | IT_GETATTR)) &&
3494             S_ISDIR(md.body->mbo_mode) && !md.default_lmv) {
3495                 if (unlikely(*inode && is_root_inode(*inode) &&
3496                              !fid_is_root(&sbi->ll_root_fid))) {
3497                         rc = ll_fileset_default_lmv_fixup(*inode, &md);
3498                         if (rc)
3499                                 GOTO(out, rc);
3500                 }
3501
3502                 if (!md.default_lmv)
3503                         default_lmv_deleted = true;
3504         }
3505
3506         if (*inode) {
3507                 rc = ll_update_inode(*inode, &md);
3508                 if (rc != 0)
3509                         GOTO(out, rc);
3510         } else {
3511                 bool api32 = test_bit(LL_SBI_32BIT_API, sbi->ll_flags);
3512                 struct lu_fid *fid1 = &md.body->mbo_fid1;
3513
3514                 LASSERT(sb != NULL);
3515
3516                 /*
3517                  * At this point server returns to client's same fid as client
3518                  * generated for creating. So using ->fid1 is okay here.
3519                  */
3520                 if (!fid_is_sane(fid1)) {
3521                         CERROR("%s: Fid is insane "DFID"\n",
3522                                 sbi->ll_fsname, PFID(fid1));
3523                         GOTO(out, rc = -EINVAL);
3524                 }
3525
3526                 *inode = ll_iget(sb, cl_fid_build_ino(fid1, api32), &md);
3527                 if (IS_ERR(*inode)) {
3528                         lmd_clear_acl(&md);
3529                         rc = IS_ERR(*inode) ? PTR_ERR(*inode) : -ENOMEM;
3530                         *inode = NULL;
3531                         CERROR("new_inode -fatal: rc %d\n", rc);
3532                         GOTO(out, rc);
3533                 }
3534         }
3535
3536         /* Handling piggyback layout lock.
3537          * Layout lock can be piggybacked by getattr and open request.
3538          * The lsm can be applied to inode only if it comes with a layout lock
3539          * otherwise correct layout may be overwritten, for example:
3540          * 1. proc1: mdt returns a lsm but not granting layout
3541          * 2. layout was changed by another client
3542          * 3. proc2: refresh layout and layout lock granted
3543          * 4. proc1: to apply a stale layout */
3544         if (it != NULL && it->it_lock_mode != 0) {
3545                 struct lustre_handle lockh;
3546                 struct ldlm_lock *lock;
3547
3548                 lockh.cookie = it->it_lock_handle;
3549                 lock = ldlm_handle2lock(&lockh);
3550                 LASSERT(lock != NULL);
3551                 if (ldlm_has_layout(lock)) {
3552                         struct cl_object_conf conf;
3553
3554                         memset(&conf, 0, sizeof(conf));
3555                         conf.coc_opc = OBJECT_CONF_SET;
3556                         conf.coc_inode = *inode;
3557                         conf.coc_lock = lock;
3558                         conf.u.coc_layout = md.layout;
3559                         (void)ll_layout_conf(*inode, &conf);
3560                 }
3561                 LDLM_LOCK_PUT(lock);
3562         }
3563
3564         if (default_lmv_deleted)
3565                 ll_update_default_lsm_md(*inode, &md);
3566
3567         /* we may want to apply some policy for foreign file/dir */
3568         if (ll_sbi_has_foreign_symlink(sbi)) {
3569                 rc = ll_manage_foreign(*inode, &md);
3570                 if (rc < 0)
3571                         GOTO(out, rc);
3572         }
3573
3574         GOTO(out, rc = 0);
3575
3576 out:
3577         /* cleanup will be done if necessary */
3578         md_free_lustre_md(sbi->ll_md_exp, &md);
3579
3580         if (rc != 0 && it != NULL && it->it_op & IT_OPEN) {
3581                 ll_intent_drop_lock(it);
3582                 ll_open_cleanup(sb != NULL ? sb : (*inode)->i_sb, pill);
3583         }
3584
3585         return rc;
3586 }
3587
3588 int ll_obd_statfs(struct inode *inode, void __user *uarg)
3589 {
3590         struct ll_sb_info *sbi = NULL;
3591         struct obd_export *exp;
3592         struct obd_ioctl_data *data = NULL;
3593         __u32 type;
3594         int len = 0, rc;
3595
3596         if (inode)
3597                 sbi = ll_i2sbi(inode);
3598         if (!sbi)
3599                 GOTO(out_statfs, rc = -EINVAL);
3600
3601         rc = obd_ioctl_getdata(&data, &len, uarg);
3602         if (rc)
3603                 GOTO(out_statfs, rc);
3604
3605         if (!data->ioc_inlbuf1 || !data->ioc_inlbuf2 ||
3606             !data->ioc_pbuf1 || !data->ioc_pbuf2)
3607                 GOTO(out_statfs, rc = -EINVAL);
3608
3609         if (data->ioc_inllen1 != sizeof(__u32) ||
3610             data->ioc_inllen2 != sizeof(__u32) ||
3611             data->ioc_plen1 != sizeof(struct obd_statfs) ||
3612             data->ioc_plen2 != sizeof(struct obd_uuid))
3613                 GOTO(out_statfs, rc = -EINVAL);
3614
3615         memcpy(&type, data->ioc_inlbuf1, sizeof(__u32));
3616         if (type & LL_STATFS_LMV)
3617                 exp = sbi->ll_md_exp;
3618         else if (type & LL_STATFS_LOV)
3619                 exp = sbi->ll_dt_exp;
3620         else
3621                 GOTO(out_statfs, rc = -ENODEV);
3622
3623         rc = obd_iocontrol(IOC_OBD_STATFS, exp, len, data, NULL);
3624         if (rc)
3625                 GOTO(out_statfs, rc);
3626 out_statfs:
3627         OBD_FREE_LARGE(data, len);
3628         return rc;
3629 }
3630
3631 /*
3632  * this is normally called in ll_fini_md_op_data(), but sometimes it needs to
3633  * be called early to avoid deadlock.
3634  */
3635 void ll_unlock_md_op_lsm(struct md_op_data *op_data)
3636 {
3637         if (op_data->op_mea2_sem) {
3638                 up_read_non_owner(op_data->op_mea2_sem);
3639                 op_data->op_mea2_sem = NULL;
3640         }
3641
3642         if (op_data->op_mea1_sem) {
3643                 up_read_non_owner(op_data->op_mea1_sem);
3644                 op_data->op_mea1_sem = NULL;
3645         }
3646 }
3647
3648 /* this function prepares md_op_data hint for passing it down to MD stack. */
3649 struct md_op_data *ll_prep_md_op_data(struct md_op_data *op_data,
3650                                       struct inode *i1, struct inode *i2,
3651                                       const char *name, size_t namelen,
3652                                       __u32 mode, enum md_op_code opc,
3653                                       void *data)
3654 {
3655         struct llcrypt_name fname = { 0 };
3656         int rc;
3657
3658         LASSERT(i1 != NULL);
3659
3660         if (name == NULL) {
3661                 /* Do not reuse namelen for something else. */
3662                 if (namelen != 0)
3663                         return ERR_PTR(-EINVAL);
3664         } else {
3665                 if ((!IS_ENCRYPTED(i1) ||
3666                      (opc != LUSTRE_OPC_LOOKUP && opc != LUSTRE_OPC_CREATE)) &&
3667                     namelen > ll_i2sbi(i1)->ll_namelen)
3668                         return ERR_PTR(-ENAMETOOLONG);
3669
3670                 /* "/" is not valid name, but it's allowed */
3671                 if (!lu_name_is_valid_2(name, namelen) &&
3672                     strncmp("/", name, namelen) != 0)
3673                         return ERR_PTR(-EINVAL);
3674         }
3675
3676         if (op_data == NULL)
3677                 OBD_ALLOC_PTR(op_data);
3678
3679         if (op_data == NULL)
3680                 return ERR_PTR(-ENOMEM);
3681
3682         ll_i2gids(op_data->op_suppgids, i1, i2);
3683         /* If the client is using a subdir mount and looks at what it sees as
3684          * /.fscrypt, interpret it as the .fscrypt dir at the root of the fs.
3685          */
3686         if (unlikely(i1->i_sb && i1->i_sb->s_root && is_root_inode(i1) &&
3687                      !fid_is_root(ll_inode2fid(i1)) &&
3688                      name && namelen == strlen(dot_fscrypt_name) &&
3689                      strncmp(name, dot_fscrypt_name, namelen) == 0))
3690                 lu_root_fid(&op_data->op_fid1);
3691         else
3692                 op_data->op_fid1 = *ll_inode2fid(i1);
3693
3694         if (S_ISDIR(i1->i_mode)) {
3695                 down_read_non_owner(&ll_i2info(i1)->lli_lsm_sem);
3696                 op_data->op_mea1_sem = &ll_i2info(i1)->lli_lsm_sem;
3697                 op_data->op_mea1 = ll_i2info(i1)->lli_lsm_md;
3698                 op_data->op_default_mea1 = ll_i2info(i1)->lli_default_lsm_md;
3699         }
3700
3701         if (i2) {
3702                 op_data->op_fid2 = *ll_inode2fid(i2);
3703                 if (S_ISDIR(i2->i_mode)) {
3704                         if (i2 != i1) {
3705                                 /* i2 is typically a child of i1, and MUST be
3706                                  * further from the root to avoid deadlocks.
3707                                  */
3708                                 down_read_non_owner(&ll_i2info(i2)->lli_lsm_sem);
3709                                 op_data->op_mea2_sem =
3710                                                 &ll_i2info(i2)->lli_lsm_sem;
3711                         }
3712                         op_data->op_mea2 = ll_i2info(i2)->lli_lsm_md;
3713                 }
3714         } else {
3715                 fid_zero(&op_data->op_fid2);
3716         }
3717
3718         if (test_bit(LL_SBI_64BIT_HASH, ll_i2sbi(i1)->ll_flags))
3719                 op_data->op_cli_flags |= CLI_HASH64;
3720
3721         if (ll_need_32bit_api(ll_i2sbi(i1)))
3722                 op_data->op_cli_flags |= CLI_API32;
3723
3724         if ((i2 && is_root_inode(i2)) ||
3725             opc == LUSTRE_OPC_LOOKUP || opc == LUSTRE_OPC_CREATE) {
3726                 /* In case of lookup, ll_setup_filename() has already been
3727                  * called in ll_lookup_it(), so just take provided name.
3728                  * Also take provided name if we are dealing with root inode.
3729                  */
3730                 fname.disk_name.name = (unsigned char *)name;
3731                 fname.disk_name.len = namelen;
3732         } else if (name && namelen) {
3733                 struct qstr dname = QSTR_INIT(name, namelen);
3734                 struct inode *dir;
3735                 struct lu_fid *pfid = NULL;
3736                 struct lu_fid fid;
3737                 int lookup;
3738
3739                 if (!S_ISDIR(i1->i_mode) && i2 && S_ISDIR(i2->i_mode)) {
3740                         /* special case when called from ll_link() */
3741                         dir = i2;
3742                         lookup = 0;
3743                 } else {
3744                         dir = i1;
3745                         lookup = (int)(opc == LUSTRE_OPC_ANY);
3746                 }
3747                 if (opc == LUSTRE_OPC_ANY && lookup)
3748                         pfid = &fid;
3749                 rc = ll_setup_filename(dir, &dname, lookup, &fname, pfid);
3750                 if (rc) {
3751                         ll_finish_md_op_data(op_data);
3752                         return ERR_PTR(rc);
3753                 }
3754                 if (pfid && !fid_is_zero(pfid)) {
3755                         if (i2 == NULL)
3756                                 op_data->op_fid2 = fid;
3757                         op_data->op_bias = MDS_FID_OP;
3758                 }
3759                 if (fname.disk_name.name &&
3760                     fname.disk_name.name != (unsigned char *)name) {
3761                         /* op_data->op_name must be freed after use */
3762                         op_data->op_flags |= MF_OPNAME_KMALLOCED;
3763                 }
3764         }
3765
3766         /* In fact LUSTRE_OPC_LOOKUP, LUSTRE_OPC_OPEN
3767          * are LUSTRE_OPC_ANY
3768          */
3769         if (opc == LUSTRE_OPC_LOOKUP || opc == LUSTRE_OPC_OPEN)
3770                 op_data->op_code = LUSTRE_OPC_ANY;
3771         else
3772                 op_data->op_code = opc;
3773         op_data->op_name = fname.disk_name.name;
3774         op_data->op_namelen = fname.disk_name.len;
3775         op_data->op_mode = mode;
3776         op_data->op_mod_time = ktime_get_real_seconds();
3777         op_data->op_fsuid = from_kuid(&init_user_ns, current_fsuid());
3778         op_data->op_fsgid = from_kgid(&init_user_ns, current_fsgid());
3779         op_data->op_cap = current_cap();
3780         op_data->op_mds = 0;
3781         if ((opc == LUSTRE_OPC_CREATE) && (name != NULL) &&
3782              filename_is_volatile(name, namelen, &op_data->op_mds)) {
3783                 op_data->op_bias |= MDS_CREATE_VOLATILE;
3784         }
3785         op_data->op_data = data;
3786
3787         return op_data;
3788 }
3789
3790 void ll_finish_md_op_data(struct md_op_data *op_data)
3791 {
3792         ll_unlock_md_op_lsm(op_data);
3793         ll_security_release_secctx(op_data->op_file_secctx,
3794                                    op_data->op_file_secctx_size,
3795                                    op_data->op_file_secctx_slot);
3796         if (op_data->op_flags & MF_OPNAME_KMALLOCED)
3797                 /* allocated via ll_setup_filename called
3798                  * from ll_prep_md_op_data
3799                  */
3800                 kfree(op_data->op_name);
3801         llcrypt_free_ctx(op_data->op_file_encctx, op_data->op_file_encctx_size);
3802         OBD_FREE_PTR(op_data);
3803 }
3804
3805 int ll_show_options(struct seq_file *seq, struct dentry *dentry)
3806 {
3807         struct ll_sb_info *sbi;
3808         int i;
3809
3810         LASSERT(seq && dentry);
3811         sbi = ll_s2sbi(dentry->d_sb);
3812
3813         if (test_bit(LL_SBI_NOLCK, sbi->ll_flags))
3814                 seq_puts(seq, "nolock");
3815
3816         for (i = 1; ll_sbi_flags_name[i].token != LL_SBI_NUM_MOUNT_OPT; i++) {
3817                 /* match_table in some cases has patterns for both enabled and
3818                  * disabled cases. Ignore 'no'xxx versions if bit is set.
3819                  */
3820                 if (test_bit(ll_sbi_flags_name[i].token, sbi->ll_flags) &&
3821                     strncmp(ll_sbi_flags_name[i].pattern, "no", 2)) {
3822                         if (ll_sbi_flags_name[i].token ==
3823                             LL_SBI_FOREIGN_SYMLINK) {
3824                                 seq_show_option(seq, "foreign_symlink",
3825                                                 sbi->ll_foreign_symlink_prefix);
3826                         } else {
3827                                 seq_printf(seq, ",%s",
3828                                            ll_sbi_flags_name[i].pattern);
3829                         }
3830
3831                         /* You can have either localflock or flock but not
3832                          * both. If localflock is set don't print flock or
3833                          * noflock.
3834                          */
3835                         if (ll_sbi_flags_name[i].token == LL_SBI_LOCALFLOCK)
3836                                 i += 2;
3837                 } else if (!test_bit(ll_sbi_flags_name[i].token, sbi->ll_flags) &&
3838                            !strncmp(ll_sbi_flags_name[i].pattern, "no", 2)) {
3839                         seq_printf(seq, ",%s",
3840                                    ll_sbi_flags_name[i].pattern);
3841                 }
3842         }
3843
3844         llcrypt_show_test_dummy_encryption(seq, ',', dentry->d_sb);
3845
3846         RETURN(0);
3847 }
3848
3849 /**
3850  * Get obd name by cmd, and copy out to user space
3851  */
3852 int ll_get_obd_name(struct inode *inode, unsigned int cmd, void __user *uarg)
3853 {
3854         struct ll_sb_info *sbi = ll_i2sbi(inode);
3855         struct obd_device *obd;
3856         ENTRY;
3857
3858         if (cmd == OBD_IOC_GETNAME_OLD || cmd == OBD_IOC_GETDTNAME)
3859                 obd = class_exp2obd(sbi->ll_dt_exp);
3860         else if (cmd == OBD_IOC_GETMDNAME)
3861                 obd = class_exp2obd(sbi->ll_md_exp);
3862         else
3863                 RETURN(-EINVAL);
3864
3865         if (!obd)
3866                 RETURN(-ENOENT);
3867
3868         if (copy_to_user(uarg, obd->obd_name, strlen(obd->obd_name) + 1))
3869                 RETURN(-EFAULT);
3870
3871         RETURN(0);
3872 }
3873
3874 struct dname_buf {
3875         struct work_struct db_work;
3876         struct dentry *db_dentry;
3877         /* Let's hope the path is not too long, 32 bytes for the work struct
3878          * on my kernel
3879          */
3880         char buf[PAGE_SIZE - sizeof(struct work_struct) - sizeof(void *)];
3881 };
3882
3883 static void ll_dput_later(struct work_struct *work)
3884 {
3885         struct dname_buf *db = container_of(work, struct dname_buf, db_work);
3886
3887         dput(db->db_dentry);
3888         free_page((unsigned long)db);
3889 }
3890
3891 static char* ll_d_path(struct dentry *dentry, char *buf, int bufsize)
3892 {
3893         char *path = NULL;
3894
3895         struct path p;
3896
3897         p.dentry = dentry;
3898         p.mnt = current->fs->root.mnt;
3899         path_get(&p);
3900         path = d_path(&p, buf, bufsize);
3901         path_put(&p);
3902         return path;
3903 }
3904
3905 void ll_dirty_page_discard_warn(struct inode *inode, int ioret)
3906 {
3907         struct dname_buf *db;
3908         char  *path = NULL;
3909         struct dentry *dentry = NULL;
3910
3911         /* this can be called inside spin lock so use GFP_ATOMIC. */
3912         db = (struct dname_buf *)__get_free_page(GFP_ATOMIC);
3913         if (db != NULL) {
3914
3915                 dentry = d_find_alias(inode);
3916                 if (dentry != NULL)
3917                         path = ll_d_path(dentry, db->buf, sizeof(db->buf));
3918         }
3919
3920         /* The below message is checked in recovery-small.sh test_24b */
3921         CDEBUG(D_WARNING,
3922                "%s: dirty page discard: %s/fid: "DFID"/%s may get corrupted "
3923                "(rc %d)\n", ll_i2sbi(inode)->ll_fsname,
3924                s2lsi(inode->i_sb)->lsi_lmd->lmd_dev,
3925                PFID(ll_inode2fid(inode)),
3926                (path && !IS_ERR(path)) ? path : "", ioret);
3927
3928         if (dentry != NULL) {
3929                 /* We cannot dput here since if we happen to be the last holder
3930                  * then we can end up waiting for page evictions that
3931                  * in turn wait for RPCs that need this instance of ptlrpcd
3932                  * (callng brw_interpret->*page_completion*->vmpage_error->here)
3933                  * LU-15340
3934                  */
3935                 INIT_WORK(&db->db_work, ll_dput_later);
3936                 db->db_dentry = dentry;
3937                 schedule_work(&db->db_work);
3938         } else {
3939                 if (db != NULL)
3940                         free_page((unsigned long)db);
3941         }
3942 }
3943
3944 ssize_t ll_copy_user_md(const struct lov_user_md __user *md,
3945                         struct lov_user_md **kbuf)
3946 {
3947         struct lov_user_md      lum;
3948         ssize_t                 lum_size;
3949         ENTRY;
3950
3951         if (copy_from_user(&lum, md, sizeof(lum)))
3952                 RETURN(-EFAULT);
3953
3954         lum_size = ll_lov_user_md_size(&lum);
3955         if (lum_size < 0)
3956                 RETURN(lum_size);
3957
3958         OBD_ALLOC_LARGE(*kbuf, lum_size);
3959         if (*kbuf == NULL)
3960                 RETURN(-ENOMEM);
3961
3962         if (copy_from_user(*kbuf, md, lum_size) != 0) {
3963                 OBD_FREE_LARGE(*kbuf, lum_size);
3964                 RETURN(-EFAULT);
3965         }
3966
3967         RETURN(lum_size);
3968 }
3969
3970 /*
3971  * Compute llite root squash state after a change of root squash
3972  * configuration setting or add/remove of a lnet nid
3973  */
3974 void ll_compute_rootsquash_state(struct ll_sb_info *sbi)
3975 {
3976         struct root_squash_info *squash = &sbi->ll_squash;
3977         int i;
3978         bool matched;
3979         struct lnet_processid id;
3980
3981         /* Update norootsquash flag */
3982         spin_lock(&squash->rsi_lock);
3983         if (list_empty(&squash->rsi_nosquash_nids))
3984                 clear_bit(LL_SBI_NOROOTSQUASH, sbi->ll_flags);
3985         else {
3986                 /* Do not apply root squash as soon as one of our NIDs is
3987                  * in the nosquash_nids list */
3988                 matched = false;
3989                 i = 0;
3990                 while (LNetGetId(i++, &id) != -ENOENT) {
3991                         if (nid_is_lo0(&id.nid))
3992                                 continue;
3993                         if (cfs_match_nid(&id.nid,
3994                                           &squash->rsi_nosquash_nids)) {
3995                                 matched = true;
3996                                 break;
3997                         }
3998                 }
3999                 if (matched)
4000                         set_bit(LL_SBI_NOROOTSQUASH, sbi->ll_flags);
4001                 else
4002                         clear_bit(LL_SBI_NOROOTSQUASH, sbi->ll_flags);
4003         }
4004         spin_unlock(&squash->rsi_lock);
4005 }
4006
4007 /**
4008  * Parse linkea content to extract information about a given hardlink
4009  *
4010  * \param[in]   ldata      - Initialized linkea data
4011  * \param[in]   linkno     - Link identifier
4012  * \param[out]  parent_fid - The entry's parent FID
4013  * \param[out]  ln         - Entry name destination buffer
4014  *
4015  * \retval 0 on success
4016  * \retval Appropriate negative error code on failure
4017  */
4018 static int ll_linkea_decode(struct linkea_data *ldata, unsigned int linkno,
4019                             struct lu_fid *parent_fid, struct lu_name *ln)
4020 {
4021         unsigned int    idx;
4022         int             rc;
4023         ENTRY;
4024
4025         rc = linkea_init_with_rec(ldata);
4026         if (rc < 0)
4027                 RETURN(rc);
4028
4029         if (linkno >= ldata->ld_leh->leh_reccount)
4030                 /* beyond last link */
4031                 RETURN(-ENODATA);
4032
4033         linkea_first_entry(ldata);
4034         for (idx = 0; ldata->ld_lee != NULL; idx++) {
4035                 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen, ln,
4036                                     parent_fid);
4037                 if (idx == linkno)
4038                         break;
4039
4040                 linkea_next_entry(ldata);
4041         }
4042
4043         if (idx < linkno)
4044                 RETURN(-ENODATA);
4045
4046         RETURN(0);
4047 }
4048
4049 /**
4050  * Get parent FID and name of an identified link. Operation is performed for
4051  * a given link number, letting the caller iterate over linkno to list one or
4052  * all links of an entry.
4053  *
4054  * \param[in]     file - File descriptor against which to perform the operation
4055  * \param[in,out] arg  - User-filled structure containing the linkno to operate
4056  *                       on and the available size. It is eventually filled with
4057  *                       the requested information or left untouched on error
4058  *
4059  * \retval - 0 on success
4060  * \retval - Appropriate negative error code on failure
4061  */
4062 int ll_getparent(struct file *file, struct getparent __user *arg)
4063 {
4064         struct inode            *inode = file_inode(file);
4065         struct linkea_data      *ldata;
4066         struct lu_buf            buf = LU_BUF_NULL;
4067         struct lu_name           ln;
4068         struct lu_fid            parent_fid;
4069         __u32                    linkno;
4070         __u32                    name_size;
4071         int                      rc;
4072
4073         ENTRY;
4074
4075         if (!capable(CAP_DAC_READ_SEARCH) &&
4076             !test_bit(LL_SBI_USER_FID2PATH, ll_i2sbi(inode)->ll_flags))
4077                 RETURN(-EPERM);
4078
4079         if (get_user(name_size, &arg->gp_name_size))
4080                 RETURN(-EFAULT);
4081
4082         if (get_user(linkno, &arg->gp_linkno))
4083                 RETURN(-EFAULT);
4084
4085         if (name_size > PATH_MAX)
4086                 RETURN(-EINVAL);
4087
4088         OBD_ALLOC(ldata, sizeof(*ldata));
4089         if (ldata == NULL)
4090                 RETURN(-ENOMEM);
4091
4092         rc = linkea_data_new(ldata, &buf);
4093         if (rc < 0)
4094                 GOTO(ldata_free, rc);
4095
4096         rc = ll_xattr_list(inode, XATTR_NAME_LINK, XATTR_TRUSTED_T, buf.lb_buf,
4097                            buf.lb_len, OBD_MD_FLXATTR);
4098         if (rc < 0)
4099                 GOTO(lb_free, rc);
4100
4101         rc = ll_linkea_decode(ldata, linkno, &parent_fid, &ln);
4102         if (rc < 0)
4103                 GOTO(lb_free, rc);
4104
4105         if (ln.ln_namelen >= name_size)
4106                 GOTO(lb_free, rc = -EOVERFLOW);
4107
4108         if (copy_to_user(&arg->gp_fid, &parent_fid, sizeof(arg->gp_fid)))
4109                 GOTO(lb_free, rc = -EFAULT);
4110
4111         if (copy_to_user(&arg->gp_name, ln.ln_name, ln.ln_namelen))
4112                 GOTO(lb_free, rc = -EFAULT);
4113
4114         if (put_user('\0', arg->gp_name + ln.ln_namelen))
4115                 GOTO(lb_free, rc = -EFAULT);
4116
4117 lb_free:
4118         lu_buf_free(&buf);
4119 ldata_free:
4120         OBD_FREE(ldata, sizeof(*ldata));
4121
4122         RETURN(rc);
4123 }