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