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