Whamcloud - gitweb
LU-15969 llite: add support for ->fileattr_get/set
[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_free_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         /* ll_cl_context initialize */
1278         INIT_LIST_HEAD(&lli->lli_lccs);
1279         seqlock_init(&lli->lli_page_inv_lock);
1280         lli->lli_inode_lock_owner = NULL;
1281 }
1282
1283 #define MAX_STRING_SIZE 128
1284
1285 #ifndef HAVE_SUPER_SETUP_BDI_NAME
1286 static int super_setup_bdi_name(struct super_block *sb, char *fmt, ...)
1287 {
1288         struct  lustre_sb_info *lsi = s2lsi(sb);
1289         char buf[MAX_STRING_SIZE];
1290         va_list args;
1291         int err;
1292
1293         err = bdi_init(&lsi->lsi_bdi);
1294         if (err)
1295                 return err;
1296
1297         lsi->lsi_flags |= LSI_BDI_INITIALIZED;
1298         lsi->lsi_bdi.capabilities = BDI_CAP_MAP_COPY;
1299         lsi->lsi_bdi.name = "lustre";
1300         va_start(args, fmt);
1301         vsnprintf(buf, MAX_STRING_SIZE, fmt, args);
1302         va_end(args);
1303         err = bdi_register(&lsi->lsi_bdi, NULL, "%s", buf);
1304         va_end(args);
1305         if (!err)
1306                 sb->s_bdi = &lsi->lsi_bdi;
1307
1308         return err;
1309 }
1310 #endif /* !HAVE_SUPER_SETUP_BDI_NAME */
1311
1312 int ll_fill_super(struct super_block *sb)
1313 {
1314         struct  lustre_profile *lprof = NULL;
1315         struct  lustre_sb_info *lsi = s2lsi(sb);
1316         struct  ll_sb_info *sbi = NULL;
1317         char    *dt = NULL, *md = NULL;
1318         char    *profilenm = get_profile_name(sb);
1319         struct config_llog_instance *cfg;
1320         /* %p for void* in printf needs 16+2 characters: 0xffffffffffffffff */
1321         const int instlen = LUSTRE_MAXINSTANCE + 2;
1322         unsigned long cfg_instance = ll_get_cfg_instance(sb);
1323         char name[MAX_STRING_SIZE];
1324         int md_len = 0;
1325         int dt_len = 0;
1326         uuid_t uuid;
1327         char *ptr;
1328         int len;
1329         int err;
1330
1331         ENTRY;
1332         /* for ASLR, to map between cfg_instance and hashed ptr */
1333         CDEBUG(D_VFSTRACE, "VFS Op: cfg_instance %s-%016lx (sb %p)\n",
1334                profilenm, cfg_instance, sb);
1335
1336         CFS_RACE(OBD_FAIL_LLITE_RACE_MOUNT);
1337
1338         OBD_ALLOC_PTR(cfg);
1339         if (cfg == NULL)
1340                 GOTO(out_free_cfg, err = -ENOMEM);
1341
1342         /* client additional sb info */
1343         lsi->lsi_llsbi = sbi = ll_init_sbi(lsi);
1344         if (IS_ERR(sbi))
1345                 GOTO(out_free_cfg, err = PTR_ERR(sbi));
1346
1347         err = ll_options(lsi->lsi_lmd->lmd_opts, sb);
1348         if (err)
1349                 GOTO(out_free_cfg, err);
1350
1351         /* LSI_FILENAME_ENC is only used by embedded llcrypt */
1352 #ifdef CONFIG_LL_ENCRYPTION
1353         if (ll_sb_has_test_dummy_encryption(sb))
1354                 /* enable filename encryption by default for dummy enc mode */
1355                 lsi->lsi_flags |= LSI_FILENAME_ENC;
1356         else
1357                 /* filename encryption is disabled by default */
1358                 lsi->lsi_flags &= ~LSI_FILENAME_ENC;
1359 #endif
1360
1361         /* kernel >= 2.6.38 store dentry operations in sb->s_d_op. */
1362         sb->s_d_op = &ll_d_ops;
1363
1364         /* UUID handling */
1365         generate_random_uuid(uuid.b);
1366         snprintf(sbi->ll_sb_uuid.uuid, sizeof(sbi->ll_sb_uuid), "%pU", uuid.b);
1367
1368         CDEBUG(D_CONFIG, "llite sb uuid: %s\n", sbi->ll_sb_uuid.uuid);
1369
1370         /* Get fsname */
1371         len = strlen(profilenm);
1372         ptr = strrchr(profilenm, '-');
1373         if (ptr && (strcmp(ptr, "-client") == 0))
1374                 len -= 7;
1375
1376         if (len > LUSTRE_MAXFSNAME) {
1377                 if (unlikely(len >= MAX_STRING_SIZE))
1378                         len = MAX_STRING_SIZE - 1;
1379                 strncpy(name, profilenm, len);
1380                 name[len] = '\0';
1381                 err = -ENAMETOOLONG;
1382                 CERROR("%s: fsname longer than %u characters: rc = %d\n",
1383                        name, LUSTRE_MAXFSNAME, err);
1384                 GOTO(out_free_cfg, err);
1385         }
1386         strncpy(sbi->ll_fsname, profilenm, len);
1387         sbi->ll_fsname[len] = '\0';
1388
1389         /* Mount info */
1390         snprintf(name, sizeof(name), "%.*s-%016lx", len,
1391                  profilenm, cfg_instance);
1392
1393         err = super_setup_bdi_name(sb, "%s", name);
1394         if (err)
1395                 GOTO(out_free_cfg, err);
1396
1397         /* disable kernel readahead */
1398         sb->s_bdi->ra_pages = 0;
1399 #ifdef HAVE_BDI_IO_PAGES
1400         sb->s_bdi->io_pages = 0;
1401 #endif
1402         sb->s_bdi->capabilities |= LL_BDI_CAP_FLAGS;
1403 #ifdef SB_I_CGROUPWB
1404         sb->s_iflags |= SB_I_CGROUPWB;
1405 #endif
1406         /* Call ll_debugfs_register_super() before lustre_process_log()
1407          * so that "llite.*.*" params can be processed correctly.
1408          */
1409         err = ll_debugfs_register_super(sb, name);
1410         if (err < 0) {
1411                 CERROR("%s: could not register mountpoint in llite: rc = %d\n",
1412                        sbi->ll_fsname, err);
1413                 err = 0;
1414         }
1415
1416         /* The cfg_instance is a value unique to this super, in case some
1417          * joker tries to mount the same fs at two mount points.
1418          */
1419         cfg->cfg_instance = cfg_instance;
1420         cfg->cfg_uuid = lsi->lsi_llsbi->ll_sb_uuid;
1421         cfg->cfg_callback = class_config_llog_handler;
1422         cfg->cfg_sub_clds = CONFIG_SUB_CLIENT;
1423         /* set up client obds */
1424         err = lustre_process_log(sb, profilenm, cfg);
1425         if (err < 0)
1426                 GOTO(out_debugfs, err);
1427
1428         /* Profile set with LCFG_MOUNTOPT so we can find our mdc and osc obds */
1429         lprof = class_get_profile(profilenm);
1430         if (lprof == NULL) {
1431                 LCONSOLE_ERROR_MSG(0x156, "The client profile '%s' could not be"
1432                                    " read from the MGS.  Does that filesystem "
1433                                    "exist?\n", profilenm);
1434                 GOTO(out_debugfs, err = -EINVAL);
1435         }
1436         CDEBUG(D_CONFIG, "Found profile %s: mdc=%s osc=%s\n", profilenm,
1437                lprof->lp_md, lprof->lp_dt);
1438
1439         dt_len = strlen(lprof->lp_dt) + instlen + 2;
1440         OBD_ALLOC(dt, dt_len);
1441         if (!dt)
1442                 GOTO(out_profile, err = -ENOMEM);
1443         snprintf(dt, dt_len - 1, "%s-%016lx", lprof->lp_dt, cfg_instance);
1444
1445         md_len = strlen(lprof->lp_md) + instlen + 2;
1446         OBD_ALLOC(md, md_len);
1447         if (!md)
1448                 GOTO(out_free_dt, err = -ENOMEM);
1449         snprintf(md, md_len - 1, "%s-%016lx", lprof->lp_md, cfg_instance);
1450
1451         /* connections, registrations, sb setup */
1452         err = client_common_fill_super(sb, md, dt);
1453         if (err < 0)
1454                 GOTO(out_free_md, err);
1455
1456         sbi->ll_client_common_fill_super_succeeded = 1;
1457
1458 out_free_md:
1459         if (md)
1460                 OBD_FREE(md, md_len);
1461 out_free_dt:
1462         if (dt)
1463                 OBD_FREE(dt, dt_len);
1464 out_profile:
1465         if (lprof)
1466                 class_put_profile(lprof);
1467 out_debugfs:
1468         if (err < 0)
1469                 ll_debugfs_unregister_super(sb);
1470 out_free_cfg:
1471         if (cfg)
1472                 OBD_FREE_PTR(cfg);
1473
1474         if (err)
1475                 ll_put_super(sb);
1476         else if (test_bit(LL_SBI_VERBOSE, sbi->ll_flags))
1477                 LCONSOLE_WARN("Mounted %s%s\n", profilenm,
1478                               sb->s_flags & SB_RDONLY ? " read-only" : "");
1479         RETURN(err);
1480 } /* ll_fill_super */
1481
1482 void ll_put_super(struct super_block *sb)
1483 {
1484         struct config_llog_instance cfg, params_cfg;
1485         struct obd_device *obd;
1486         struct lustre_sb_info *lsi = s2lsi(sb);
1487         struct ll_sb_info *sbi = ll_s2sbi(sb);
1488         char *profilenm = get_profile_name(sb);
1489         unsigned long cfg_instance = ll_get_cfg_instance(sb);
1490         long ccc_count;
1491         int next, force = 1, rc = 0;
1492         ENTRY;
1493
1494         if (IS_ERR(sbi))
1495                 GOTO(out_no_sbi, 0);
1496
1497         /* Should replace instance_id with something better for ASLR */
1498         CDEBUG(D_VFSTRACE, "VFS Op: cfg_instance %s-%016lx (sb %p)\n",
1499                profilenm, cfg_instance, sb);
1500
1501         cfg.cfg_instance = cfg_instance;
1502         lustre_end_log(sb, profilenm, &cfg);
1503
1504         params_cfg.cfg_instance = cfg_instance;
1505         lustre_end_log(sb, PARAMS_FILENAME, &params_cfg);
1506
1507         if (sbi->ll_md_exp) {
1508                 obd = class_exp2obd(sbi->ll_md_exp);
1509                 if (obd)
1510                         force = obd->obd_force;
1511         }
1512
1513         /* Wait for unstable pages to be committed to stable storage */
1514         if (force == 0) {
1515                 rc = l_wait_event_abortable(
1516                         sbi->ll_cache->ccc_unstable_waitq,
1517                         atomic_long_read(&sbi->ll_cache->ccc_unstable_nr) == 0);
1518         }
1519
1520         ccc_count = atomic_long_read(&sbi->ll_cache->ccc_unstable_nr);
1521         if (force == 0 && rc != -ERESTARTSYS)
1522                 LASSERTF(ccc_count == 0, "count: %li\n", ccc_count);
1523
1524         /* We need to set force before the lov_disconnect in
1525          * lustre_common_put_super, since l_d cleans up osc's as well.
1526          */
1527         if (force) {
1528                 next = 0;
1529                 while ((obd = class_devices_in_group(&sbi->ll_sb_uuid,
1530                                                      &next)) != NULL) {
1531                         obd->obd_force = force;
1532                 }
1533         }
1534
1535         if (sbi->ll_client_common_fill_super_succeeded) {
1536                 /* Only if client_common_fill_super succeeded */
1537                 client_common_put_super(sb);
1538         }
1539
1540         /* imitate failed cleanup */
1541         if (CFS_FAIL_CHECK(OBD_FAIL_OBD_CLEANUP))
1542                 goto skip_cleanup;
1543
1544         next = 0;
1545         while ((obd = class_devices_in_group(&sbi->ll_sb_uuid, &next)))
1546                 class_manual_cleanup(obd);
1547
1548 skip_cleanup:
1549         if (test_bit(LL_SBI_VERBOSE, sbi->ll_flags))
1550                 LCONSOLE_WARN("Unmounted %s\n", profilenm ? profilenm : "");
1551
1552         if (profilenm)
1553                 class_del_profile(profilenm);
1554
1555 #ifndef HAVE_SUPER_SETUP_BDI_NAME
1556         if (lsi->lsi_flags & LSI_BDI_INITIALIZED) {
1557                 bdi_destroy(&lsi->lsi_bdi);
1558                 lsi->lsi_flags &= ~LSI_BDI_INITIALIZED;
1559         }
1560 #endif
1561
1562         llcrypt_free_dummy_policy(&lsi->lsi_dummy_enc_policy);
1563         ll_free_sbi(sb);
1564         lsi->lsi_llsbi = NULL;
1565 out_no_sbi:
1566         lustre_common_put_super(sb);
1567
1568         cl_env_cache_purge(~0);
1569
1570         EXIT;
1571 } /* client_put_super */
1572
1573 struct inode *ll_inode_from_resource_lock(struct ldlm_lock *lock)
1574 {
1575         struct inode *inode = NULL;
1576
1577         /* NOTE: we depend on atomic igrab() -bzzz */
1578         lock_res_and_lock(lock);
1579         if (lock->l_resource->lr_lvb_inode) {
1580                 struct ll_inode_info * lli;
1581                 lli = ll_i2info(lock->l_resource->lr_lvb_inode);
1582                 if (lli->lli_inode_magic == LLI_INODE_MAGIC) {
1583                         inode = igrab(lock->l_resource->lr_lvb_inode);
1584                 } else {
1585                         inode = lock->l_resource->lr_lvb_inode;
1586                         LDLM_DEBUG_LIMIT(inode->i_state & I_FREEING ?  D_INFO :
1587                                          D_WARNING, lock, "lr_lvb_inode %p is "
1588                                          "bogus: magic %08x",
1589                                          lock->l_resource->lr_lvb_inode,
1590                                          lli->lli_inode_magic);
1591                         inode = NULL;
1592                 }
1593         }
1594         unlock_res_and_lock(lock);
1595         return inode;
1596 }
1597
1598 void ll_dir_clear_lsm_md(struct inode *inode)
1599 {
1600         struct ll_inode_info *lli = ll_i2info(inode);
1601
1602         LASSERT(S_ISDIR(inode->i_mode));
1603
1604         if (lli->lli_lsm_md) {
1605                 lmv_free_memmd(lli->lli_lsm_md);
1606                 lli->lli_lsm_md = NULL;
1607         }
1608
1609         if (lli->lli_default_lsm_md) {
1610                 lmv_free_memmd(lli->lli_default_lsm_md);
1611                 lli->lli_default_lsm_md = NULL;
1612         }
1613 }
1614
1615 static struct inode *ll_iget_anon_dir(struct super_block *sb,
1616                                       const struct lu_fid *fid,
1617                                       struct lustre_md *md)
1618 {
1619         struct ll_sb_info *sbi = ll_s2sbi(sb);
1620         struct ll_inode_info *lli;
1621         struct mdt_body *body = md->body;
1622         struct inode *inode;
1623         ino_t ino;
1624
1625         ENTRY;
1626
1627         LASSERT(md->lmv);
1628         ino = cl_fid_build_ino(fid, test_bit(LL_SBI_32BIT_API, sbi->ll_flags));
1629         inode = iget_locked(sb, ino);
1630         if (inode == NULL) {
1631                 CERROR("%s: failed get simple inode "DFID": rc = -ENOENT\n",
1632                        sbi->ll_fsname, PFID(fid));
1633                 RETURN(ERR_PTR(-ENOENT));
1634         }
1635
1636         lli = ll_i2info(inode);
1637         if (inode->i_state & I_NEW) {
1638                 inode->i_mode = (inode->i_mode & ~S_IFMT) |
1639                                 (body->mbo_mode & S_IFMT);
1640                 LASSERTF(S_ISDIR(inode->i_mode), "Not slave inode "DFID"\n",
1641                          PFID(fid));
1642
1643                 inode->i_mtime.tv_sec = 0;
1644                 inode->i_atime.tv_sec = 0;
1645                 inode->i_ctime.tv_sec = 0;
1646                 inode->i_rdev = 0;
1647
1648 #ifdef HAVE_BACKING_DEV_INFO
1649                 /* initializing backing dev info. */
1650                 inode->i_mapping->backing_dev_info =
1651                                                 &s2lsi(inode->i_sb)->lsi_bdi;
1652 #endif
1653                 inode->i_op = &ll_dir_inode_operations;
1654                 inode->i_fop = &ll_dir_operations;
1655                 lli->lli_fid = *fid;
1656                 ll_lli_init(lli);
1657
1658                 /* master object FID */
1659                 lli->lli_pfid = body->mbo_fid1;
1660                 CDEBUG(D_INODE, "lli %p slave "DFID" master "DFID"\n",
1661                        lli, PFID(fid), PFID(&lli->lli_pfid));
1662                 unlock_new_inode(inode);
1663         } else {
1664                 /* in directory restripe/auto-split, a directory will be
1665                  * transformed to a stripe if it's plain, set its pfid here,
1666                  * otherwise ll_lock_cancel_bits() can't find the master inode.
1667                  */
1668                 lli->lli_pfid = body->mbo_fid1;
1669         }
1670
1671         RETURN(inode);
1672 }
1673
1674 static int ll_init_lsm_md(struct inode *inode, struct lustre_md *md)
1675 {
1676         struct lu_fid *fid;
1677         struct lmv_stripe_md *lsm = md->lmv;
1678         struct ll_inode_info *lli = ll_i2info(inode);
1679         int i;
1680
1681         LASSERT(lsm != NULL);
1682
1683         CDEBUG(D_INODE, "%s: "DFID" set dir layout:\n",
1684                ll_i2sbi(inode)->ll_fsname, PFID(&lli->lli_fid));
1685         lsm_md_dump(D_INODE, lsm);
1686
1687         if (!lmv_dir_striped(lsm))
1688                 goto out;
1689
1690         /* XXX sigh, this lsm_root initialization should be in
1691          * LMV layer, but it needs ll_iget right now, so we
1692          * put this here right now. */
1693         for (i = 0; i < lsm->lsm_md_stripe_count; i++) {
1694                 fid = &lsm->lsm_md_oinfo[i].lmo_fid;
1695                 LASSERT(lsm->lsm_md_oinfo[i].lmo_root == NULL);
1696
1697                 if (!fid_is_sane(fid))
1698                         continue;
1699
1700                 /* Unfortunately ll_iget will call ll_update_inode,
1701                  * where the initialization of slave inode is slightly
1702                  * different, so it reset lsm_md to NULL to avoid
1703                  * initializing lsm for slave inode. */
1704                 lsm->lsm_md_oinfo[i].lmo_root =
1705                                 ll_iget_anon_dir(inode->i_sb, fid, md);
1706                 if (IS_ERR(lsm->lsm_md_oinfo[i].lmo_root)) {
1707                         int rc = PTR_ERR(lsm->lsm_md_oinfo[i].lmo_root);
1708
1709                         lsm->lsm_md_oinfo[i].lmo_root = NULL;
1710                         while (i-- > 0) {
1711                                 iput(lsm->lsm_md_oinfo[i].lmo_root);
1712                                 lsm->lsm_md_oinfo[i].lmo_root = NULL;
1713                         }
1714                         return rc;
1715                 }
1716         }
1717 out:
1718         lli->lli_lsm_md = lsm;
1719
1720         return 0;
1721 }
1722
1723 static void ll_update_default_lsm_md(struct inode *inode, struct lustre_md *md)
1724 {
1725         struct ll_inode_info *lli = ll_i2info(inode);
1726
1727         ENTRY;
1728
1729         if (!md->default_lmv) {
1730                 /* clear default lsm */
1731                 if (lli->lli_default_lsm_md && lli->lli_default_lmv_set) {
1732                         down_write(&lli->lli_lsm_sem);
1733                         if (lli->lli_default_lsm_md &&
1734                             lli->lli_default_lmv_set) {
1735                                 lmv_free_memmd(lli->lli_default_lsm_md);
1736                                 lli->lli_default_lsm_md = NULL;
1737                                 lli->lli_inherit_depth = 0;
1738                                 lli->lli_default_lmv_set = 0;
1739                         }
1740                         up_write(&lli->lli_lsm_sem);
1741                 }
1742                 RETURN_EXIT;
1743         }
1744
1745         if (lli->lli_default_lsm_md) {
1746                 /* do nonthing if default lsm isn't changed */
1747                 down_read(&lli->lli_lsm_sem);
1748                 if (lli->lli_default_lsm_md &&
1749                     lsm_md_eq(lli->lli_default_lsm_md, md->default_lmv)) {
1750                         up_read(&lli->lli_lsm_sem);
1751                         RETURN_EXIT;
1752                 }
1753                 up_read(&lli->lli_lsm_sem);
1754         }
1755
1756         down_write(&lli->lli_lsm_sem);
1757         if (lli->lli_default_lsm_md)
1758                 lmv_free_memmd(lli->lli_default_lsm_md);
1759         lli->lli_default_lsm_md = md->default_lmv;
1760         lli->lli_default_lmv_set = 1;
1761         lsm_md_dump(D_INODE, md->default_lmv);
1762         md->default_lmv = NULL;
1763         up_write(&lli->lli_lsm_sem);
1764         RETURN_EXIT;
1765 }
1766
1767 static int ll_update_lsm_md(struct inode *inode, struct lustre_md *md)
1768 {
1769         struct ll_inode_info *lli = ll_i2info(inode);
1770         struct lmv_stripe_md *lsm = md->lmv;
1771         struct cl_attr  *attr;
1772         int rc = 0;
1773
1774         ENTRY;
1775
1776         LASSERT(S_ISDIR(inode->i_mode));
1777         CDEBUG(D_INODE, "update lsm %p of "DFID"\n", lli->lli_lsm_md,
1778                PFID(ll_inode2fid(inode)));
1779
1780         /* update default LMV */
1781         if (md->default_lmv)
1782                 ll_update_default_lsm_md(inode, md);
1783
1784         /* after dir migration/restripe, a stripe may be turned into a
1785          * directory, in this case, zero out its lli_pfid.
1786          */
1787         if (unlikely(fid_is_norm(&lli->lli_pfid)))
1788                 fid_zero(&lli->lli_pfid);
1789
1790         /*
1791          * no striped information from request, lustre_md from req does not
1792          * include stripeEA, see ll_md_setattr()
1793          */
1794         if (!lsm)
1795                 RETURN(0);
1796
1797         /*
1798          * normally dir layout doesn't change, only take read lock to check
1799          * that to avoid blocking other MD operations.
1800          */
1801         down_read(&lli->lli_lsm_sem);
1802
1803         /* some current lookup initialized lsm, and unchanged */
1804         if (lli->lli_lsm_md && lsm_md_eq(lli->lli_lsm_md, lsm))
1805                 GOTO(unlock, rc = 0);
1806
1807         /* if dir layout doesn't match, check whether version is increased,
1808          * which means layout is changed, this happens in dir split/merge and
1809          * lfsck.
1810          *
1811          * foreign LMV should not change.
1812          */
1813         if (lli->lli_lsm_md && lmv_dir_striped(lli->lli_lsm_md) &&
1814             lsm->lsm_md_layout_version <=
1815             lli->lli_lsm_md->lsm_md_layout_version) {
1816                 CERROR("%s: "DFID" dir layout mismatch:\n",
1817                        ll_i2sbi(inode)->ll_fsname, PFID(&lli->lli_fid));
1818                 lsm_md_dump(D_ERROR, lli->lli_lsm_md);
1819                 lsm_md_dump(D_ERROR, lsm);
1820                 GOTO(unlock, rc = -EINVAL);
1821         }
1822
1823         up_read(&lli->lli_lsm_sem);
1824         down_write(&lli->lli_lsm_sem);
1825         /* clear existing lsm */
1826         if (lli->lli_lsm_md) {
1827                 lmv_free_memmd(lli->lli_lsm_md);
1828                 lli->lli_lsm_md = NULL;
1829         }
1830
1831         rc = ll_init_lsm_md(inode, md);
1832         if (rc) {
1833                 up_write(&lli->lli_lsm_sem);
1834                 RETURN(rc);
1835         }
1836
1837         /* md_merge_attr() may take long, since lsm is already set, switch to
1838          * read lock.
1839          */
1840         downgrade_write(&lli->lli_lsm_sem);
1841
1842         /* set md->lmv to NULL, so the following free lustre_md will not free
1843          * this lsm.
1844          */
1845         md->lmv = NULL;
1846
1847         if (!lmv_dir_striped(lli->lli_lsm_md))
1848                 GOTO(unlock, rc = 0);
1849
1850         OBD_ALLOC_PTR(attr);
1851         if (!attr)
1852                 GOTO(unlock, rc = -ENOMEM);
1853
1854         /* validate the lsm */
1855         rc = md_merge_attr(ll_i2mdexp(inode), lli->lli_lsm_md, attr,
1856                            ll_md_blocking_ast);
1857         if (!rc) {
1858                 if (md->body->mbo_valid & OBD_MD_FLNLINK)
1859                         md->body->mbo_nlink = attr->cat_nlink;
1860                 if (md->body->mbo_valid & OBD_MD_FLSIZE)
1861                         md->body->mbo_size = attr->cat_size;
1862                 if (md->body->mbo_valid & OBD_MD_FLATIME)
1863                         md->body->mbo_atime = attr->cat_atime;
1864                 if (md->body->mbo_valid & OBD_MD_FLCTIME)
1865                         md->body->mbo_ctime = attr->cat_ctime;
1866                 if (md->body->mbo_valid & OBD_MD_FLMTIME)
1867                         md->body->mbo_mtime = attr->cat_mtime;
1868         }
1869
1870         OBD_FREE_PTR(attr);
1871         GOTO(unlock, rc);
1872 unlock:
1873         up_read(&lli->lli_lsm_sem);
1874
1875         return rc;
1876 }
1877
1878 void ll_clear_inode(struct inode *inode)
1879 {
1880         struct ll_inode_info *lli = ll_i2info(inode);
1881         struct ll_sb_info *sbi = ll_i2sbi(inode);
1882
1883         ENTRY;
1884
1885         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
1886                PFID(ll_inode2fid(inode)), inode);
1887
1888         if (S_ISDIR(inode->i_mode)) {
1889                 /* these should have been cleared in ll_file_release */
1890                 LASSERT(lli->lli_opendir_key == NULL);
1891                 LASSERT(lli->lli_sai == NULL);
1892                 LASSERT(lli->lli_opendir_pid == 0);
1893         } else {
1894                 pcc_inode_free(inode);
1895         }
1896
1897         md_null_inode(sbi->ll_md_exp, ll_inode2fid(inode));
1898
1899         LASSERT(!lli->lli_open_fd_write_count);
1900         LASSERT(!lli->lli_open_fd_read_count);
1901         LASSERT(!lli->lli_open_fd_exec_count);
1902
1903         if (lli->lli_mds_write_och)
1904                 ll_md_real_close(inode, FMODE_WRITE);
1905         if (lli->lli_mds_exec_och)
1906                 ll_md_real_close(inode, FMODE_EXEC);
1907         if (lli->lli_mds_read_och)
1908                 ll_md_real_close(inode, FMODE_READ);
1909
1910         if (S_ISLNK(inode->i_mode) && lli->lli_symlink_name) {
1911                 OBD_FREE(lli->lli_symlink_name,
1912                          strlen(lli->lli_symlink_name) + 1);
1913                 lli->lli_symlink_name = NULL;
1914         }
1915
1916         ll_xattr_cache_destroy(inode);
1917
1918         forget_all_cached_acls(inode);
1919         lli_clear_acl(lli);
1920         lli->lli_inode_magic = LLI_INODE_DEAD;
1921
1922         if (S_ISDIR(inode->i_mode))
1923                 ll_dir_clear_lsm_md(inode);
1924         else if (S_ISREG(inode->i_mode) && !is_bad_inode(inode))
1925                 LASSERT(list_empty(&lli->lli_agl_list));
1926
1927         /*
1928          * XXX This has to be done before lsm is freed below, because
1929          * cl_object still uses inode lsm.
1930          */
1931         cl_inode_fini(inode);
1932
1933         llcrypt_put_encryption_info(inode);
1934
1935         EXIT;
1936 }
1937
1938 static int ll_md_setattr(struct dentry *dentry, struct md_op_data *op_data)
1939 {
1940         struct lustre_md md;
1941         struct inode *inode = dentry->d_inode;
1942         struct ll_sb_info *sbi = ll_i2sbi(inode);
1943         struct ptlrpc_request *request = NULL;
1944         int rc, ia_valid;
1945
1946         ENTRY;
1947
1948         op_data = ll_prep_md_op_data(op_data, inode, NULL, NULL, 0, 0,
1949                                      LUSTRE_OPC_ANY, NULL);
1950         if (IS_ERR(op_data))
1951                 RETURN(PTR_ERR(op_data));
1952
1953         /* If this is a chgrp of a regular file, we want to reserve enough
1954          * quota to cover the entire file size.
1955          */
1956         if (S_ISREG(inode->i_mode) && op_data->op_attr.ia_valid & ATTR_GID &&
1957             from_kgid(&init_user_ns, op_data->op_attr.ia_gid) !=
1958             from_kgid(&init_user_ns, inode->i_gid)) {
1959                 op_data->op_xvalid |= OP_XVALID_BLOCKS;
1960                 op_data->op_attr_blocks = inode->i_blocks;
1961         }
1962
1963
1964         rc = md_setattr(sbi->ll_md_exp, op_data, NULL, 0, &request);
1965         if (rc) {
1966                 ptlrpc_req_finished(request);
1967                 if (rc == -ENOENT) {
1968                         clear_nlink(inode);
1969                         /* Unlinked special device node? Or just a race?
1970                          * Pretend we done everything. */
1971                         if (!S_ISREG(inode->i_mode) &&
1972                             !S_ISDIR(inode->i_mode)) {
1973                                 ia_valid = op_data->op_attr.ia_valid;
1974                                 op_data->op_attr.ia_valid &= ~TIMES_SET_FLAGS;
1975                                 rc = simple_setattr(&init_user_ns, dentry,
1976                                                     &op_data->op_attr);
1977                                 op_data->op_attr.ia_valid = ia_valid;
1978                         }
1979                 } else if (rc != -EPERM && rc != -EACCES && rc != -ETXTBSY) {
1980                         CERROR("md_setattr fails: rc = %d\n", rc);
1981                 }
1982                 RETURN(rc);
1983         }
1984
1985         rc = md_get_lustre_md(sbi->ll_md_exp, &request->rq_pill, sbi->ll_dt_exp,
1986                               sbi->ll_md_exp, &md);
1987         if (rc) {
1988                 ptlrpc_req_finished(request);
1989                 RETURN(rc);
1990         }
1991
1992         ia_valid = op_data->op_attr.ia_valid;
1993         /* inode size will be in ll_setattr_ost, can't do it now since dirty
1994          * cache is not cleared yet. */
1995         op_data->op_attr.ia_valid &= ~(TIMES_SET_FLAGS | ATTR_SIZE);
1996         if (S_ISREG(inode->i_mode))
1997                 ll_inode_lock(inode);
1998         rc = simple_setattr(&init_user_ns, dentry, &op_data->op_attr);
1999         if (S_ISREG(inode->i_mode))
2000                 ll_inode_unlock(inode);
2001         op_data->op_attr.ia_valid = ia_valid;
2002
2003         rc = ll_update_inode(inode, &md);
2004         ptlrpc_req_finished(request);
2005
2006         RETURN(rc);
2007 }
2008
2009 /**
2010  * Zero portion of page that is part of @inode.
2011  * This implies, if necessary:
2012  * - taking cl_lock on range corresponding to concerned page
2013  * - grabbing vm page
2014  * - associating cl_page
2015  * - proceeding to clio read
2016  * - zeroing range in page
2017  * - proceeding to cl_page flush
2018  * - releasing cl_lock
2019  *
2020  * \param[in] inode     inode
2021  * \param[in] index     page index
2022  * \param[in] offset    offset in page to start zero from
2023  * \param[in] len       len to zero
2024  *
2025  * \retval 0            on success
2026  * \retval negative     errno on failure
2027  */
2028 static int ll_io_zero_page(struct inode *inode, pgoff_t index, pgoff_t offset,
2029                            unsigned int len)
2030 {
2031         struct ll_inode_info *lli = ll_i2info(inode);
2032         struct cl_object *clob = lli->lli_clob;
2033         __u16 refcheck;
2034         struct lu_env *env = NULL;
2035         struct cl_io *io = NULL;
2036         struct cl_page *clpage = NULL;
2037         struct page *vmpage = NULL;
2038         unsigned from = index << PAGE_SHIFT;
2039         struct cl_lock *lock = NULL;
2040         struct cl_lock_descr *descr = NULL;
2041         struct cl_2queue *queue = NULL;
2042         struct cl_sync_io *anchor = NULL;
2043         bool holdinglock = false;
2044         int rc;
2045
2046         ENTRY;
2047
2048         env = cl_env_get(&refcheck);
2049         if (IS_ERR(env))
2050                 RETURN(PTR_ERR(env));
2051
2052         io = vvp_env_thread_io(env);
2053         io->ci_obj = clob;
2054         rc = cl_io_rw_init(env, io, CIT_WRITE, from, PAGE_SIZE);
2055         if (rc)
2056                 GOTO(putenv, rc);
2057
2058         lock = vvp_env_lock(env);
2059         descr = &lock->cll_descr;
2060         descr->cld_obj   = io->ci_obj;
2061         descr->cld_start = from >> PAGE_SHIFT;
2062         descr->cld_end   = (from + PAGE_SIZE - 1) >> PAGE_SHIFT;
2063         descr->cld_mode  = CLM_WRITE;
2064         descr->cld_enq_flags = CEF_MUST | CEF_NONBLOCK;
2065
2066         /* request lock for page */
2067         rc = cl_lock_request(env, io, lock);
2068         /* -ECANCELED indicates a matching lock with a different extent
2069          * was already present, and -EEXIST indicates a matching lock
2070          * on exactly the same extent was already present.
2071          * In both cases it means we are covered.
2072          */
2073         if (rc == -ECANCELED || rc == -EEXIST)
2074                 rc = 0;
2075         else if (rc < 0)
2076                 GOTO(iofini, rc);
2077         else
2078                 holdinglock = true;
2079
2080         /* grab page */
2081         vmpage = grab_cache_page_nowait(inode->i_mapping, index);
2082         if (vmpage == NULL)
2083                 GOTO(rellock, rc = -EOPNOTSUPP);
2084
2085         if (!PageDirty(vmpage)) {
2086                 /* associate cl_page */
2087                 clpage = cl_page_find(env, clob, vmpage->index,
2088                                       vmpage, CPT_CACHEABLE);
2089                 if (IS_ERR(clpage))
2090                         GOTO(pagefini, rc = PTR_ERR(clpage));
2091
2092                 cl_page_assume(env, io, clpage);
2093         }
2094
2095         if (!PageUptodate(vmpage) && !PageDirty(vmpage) &&
2096             !PageWriteback(vmpage)) {
2097                 /* read page */
2098                 /* Set PagePrivate2 to detect special case of empty page
2099                  * in osc_brw_fini_request().
2100                  * It is also used to tell ll_io_read_page() that we do not
2101                  * want the vmpage to be unlocked.
2102                  */
2103                 SetPagePrivate2(vmpage);
2104                 rc = ll_io_read_page(env, io, clpage, NULL);
2105                 if (!PagePrivate2(vmpage)) {
2106                         /* PagePrivate2 was cleared in osc_brw_fini_request()
2107                          * meaning we read an empty page. In this case, in order
2108                          * to avoid allocating unnecessary block in truncated
2109                          * file, we must not zero and write as below. Subsequent
2110                          * server-side truncate will handle things correctly.
2111                          */
2112                         cl_page_unassume(env, io, clpage);
2113                         GOTO(clpfini, rc = 0);
2114                 }
2115                 ClearPagePrivate2(vmpage);
2116                 if (rc)
2117                         GOTO(clpfini, rc);
2118         }
2119
2120         /* Thanks to PagePrivate2 flag, ll_io_read_page() did not unlock
2121          * the vmpage, so we are good to proceed and zero range in page.
2122          */
2123         zero_user(vmpage, offset, len);
2124
2125         if (holdinglock && clpage) {
2126                 /* explicitly write newly modified page */
2127                 queue = &io->ci_queue;
2128                 cl_2queue_init(queue);
2129                 anchor = &vvp_env_info(env)->vti_anchor;
2130                 cl_sync_io_init(anchor, 1);
2131                 clpage->cp_sync_io = anchor;
2132                 cl_page_list_add(&queue->c2_qin, clpage, true);
2133                 rc = cl_io_submit_rw(env, io, CRT_WRITE, queue);
2134                 if (rc)
2135                         GOTO(queuefini1, rc);
2136                 rc = cl_sync_io_wait(env, anchor, 0);
2137                 if (rc)
2138                         GOTO(queuefini2, rc);
2139                 cl_page_assume(env, io, clpage);
2140
2141 queuefini2:
2142                 cl_2queue_discard(env, io, queue);
2143 queuefini1:
2144                 cl_2queue_disown(env, queue);
2145                 cl_2queue_fini(env, queue);
2146         }
2147
2148 clpfini:
2149         if (clpage)
2150                 cl_page_put(env, clpage);
2151 pagefini:
2152         unlock_page(vmpage);
2153         put_page(vmpage);
2154 rellock:
2155         if (holdinglock)
2156                 cl_lock_release(env, lock);
2157 iofini:
2158         cl_io_fini(env, io);
2159 putenv:
2160         if (env)
2161                 cl_env_put(env, &refcheck);
2162
2163         RETURN(rc);
2164 }
2165
2166 /**
2167  * Get reference file from volatile file name.
2168  * Volatile file name may look like:
2169  * <parent>/LUSTRE_VOLATILE_HDR:<mdt_index>:<random>:fd=<fd>
2170  * where fd is opened descriptor of reference file.
2171  *
2172  * \param[in] volatile_name     volatile file name
2173  * \param[in] volatile_len      volatile file name length
2174  * \param[out] ref_file         pointer to struct file of reference file
2175  *
2176  * \retval 0            on success
2177  * \retval negative     errno on failure
2178  */
2179 int volatile_ref_file(const char *volatile_name, int volatile_len,
2180                       struct file **ref_file)
2181 {
2182         char *p, *q, *fd_str;
2183         int fd, rc;
2184
2185         p = strnstr(volatile_name, ":fd=", volatile_len);
2186         if (!p || strlen(p + 4) == 0)
2187                 return -EINVAL;
2188
2189         q = strchrnul(p + 4, ':');
2190         fd_str = kstrndup(p + 4, q - p - 4, GFP_NOFS);
2191         if (!fd_str)
2192                 return -ENOMEM;
2193         rc = kstrtouint(fd_str, 10, &fd);
2194         kfree(fd_str);
2195         if (rc)
2196                 return -EINVAL;
2197
2198         *ref_file = fget(fd);
2199         if (!(*ref_file))
2200                 return -EINVAL;
2201         return 0;
2202 }
2203
2204 /* If this inode has objects allocated to it (lsm != NULL), then the OST
2205  * object(s) determine the file size and mtime.  Otherwise, the MDS will
2206  * keep these values until such a time that objects are allocated for it.
2207  * We do the MDS operations first, as it is checking permissions for us.
2208  * We don't to the MDS RPC if there is nothing that we want to store there,
2209  * otherwise there is no harm in updating mtime/atime on the MDS if we are
2210  * going to do an RPC anyways.
2211  *
2212  * If we are doing a truncate, we will send the mtime and ctime updates
2213  * to the OST with the punch RPC, otherwise we do an explicit setattr RPC.
2214  * I don't believe it is possible to get e.g. ATTR_MTIME_SET and ATTR_SIZE
2215  * at the same time.
2216  *
2217  * In case of HSMimport, we only set attr on MDS.
2218  */
2219 int ll_setattr_raw(struct dentry *dentry, struct iattr *attr,
2220                    enum op_xvalid xvalid, bool hsm_import)
2221 {
2222         struct inode *inode = dentry->d_inode;
2223         struct ll_inode_info *lli = ll_i2info(inode);
2224         struct md_op_data *op_data = NULL;
2225         ktime_t kstart = ktime_get();
2226         int rc = 0;
2227
2228         ENTRY;
2229
2230         /* VFS has locked the inode before calling this */
2231         ll_set_inode_lock_owner(inode);
2232
2233         CDEBUG(D_VFSTRACE, "%s: setattr inode "DFID"(%p) from %llu to %llu, "
2234                "valid %x, hsm_import %d\n",
2235                ll_i2sbi(inode)->ll_fsname, PFID(&lli->lli_fid),
2236                inode, i_size_read(inode), attr->ia_size, attr->ia_valid,
2237                hsm_import);
2238
2239         if (attr->ia_valid & ATTR_SIZE) {
2240                 /* Check new size against VFS/VM file size limit and rlimit */
2241                 rc = inode_newsize_ok(inode, attr->ia_size);
2242                 if (rc)
2243                         GOTO(clear, rc);
2244
2245                 /* The maximum Lustre file size is variable, based on the
2246                  * OST maximum object size and number of stripes.  This
2247                  * needs another check in addition to the VFS check above. */
2248                 if (attr->ia_size > ll_file_maxbytes(inode)) {
2249                         CDEBUG(D_INODE,"file "DFID" too large %llu > %llu\n",
2250                                PFID(&lli->lli_fid), attr->ia_size,
2251                                ll_file_maxbytes(inode));
2252                         GOTO(clear, rc = -EFBIG);
2253                 }
2254
2255                 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
2256         }
2257
2258         /* POSIX: check before ATTR_*TIME_SET set (from inode_change_ok) */
2259         if (attr->ia_valid & TIMES_SET_FLAGS) {
2260                 if ((!uid_eq(current_fsuid(), inode->i_uid)) &&
2261                     !capable(CAP_FOWNER))
2262                         GOTO(clear, rc = -EPERM);
2263         }
2264
2265         /* We mark all of the fields "set" so MDS/OST does not re-set them */
2266         if (!(xvalid & OP_XVALID_CTIME_SET) &&
2267              (attr->ia_valid & ATTR_CTIME)) {
2268                 attr->ia_ctime = current_time(inode);
2269                 xvalid |= OP_XVALID_CTIME_SET;
2270         }
2271         if (!(attr->ia_valid & ATTR_ATIME_SET) &&
2272             (attr->ia_valid & ATTR_ATIME)) {
2273                 attr->ia_atime = current_time(inode);
2274                 attr->ia_valid |= ATTR_ATIME_SET;
2275         }
2276         if (!(attr->ia_valid & ATTR_MTIME_SET) &&
2277             (attr->ia_valid & ATTR_MTIME)) {
2278                 attr->ia_mtime = current_time(inode);
2279                 attr->ia_valid |= ATTR_MTIME_SET;
2280         }
2281
2282         if (attr->ia_valid & (ATTR_MTIME | ATTR_CTIME))
2283                 CDEBUG(D_INODE, "setting mtime %lld, ctime %lld, now = %lld\n",
2284                        (s64)attr->ia_mtime.tv_sec, (s64)attr->ia_ctime.tv_sec,
2285                        ktime_get_real_seconds());
2286
2287         if (S_ISREG(inode->i_mode))
2288                 ll_inode_unlock(inode);
2289
2290         /* We always do an MDS RPC, even if we're only changing the size;
2291          * only the MDS knows whether truncate() should fail with -ETXTBUSY */
2292
2293         OBD_ALLOC_PTR(op_data);
2294         if (op_data == NULL)
2295                 GOTO(out, rc = -ENOMEM);
2296
2297         if (!hsm_import && attr->ia_valid & ATTR_SIZE) {
2298                 /* If we are changing file size, file content is
2299                  * modified, flag it.
2300                  */
2301                 xvalid |= OP_XVALID_OWNEROVERRIDE;
2302                 op_data->op_bias |= MDS_DATA_MODIFIED;
2303                 clear_bit(LLIF_DATA_MODIFIED, &lli->lli_flags);
2304         }
2305
2306         if (attr->ia_valid & ATTR_FILE) {
2307                 struct ll_file_data *fd = attr->ia_file->private_data;
2308
2309                 if (fd->fd_lease_och)
2310                         op_data->op_bias |= MDS_TRUNC_KEEP_LEASE;
2311         }
2312
2313         op_data->op_attr = *attr;
2314         op_data->op_xvalid = xvalid;
2315
2316         rc = ll_md_setattr(dentry, op_data);
2317         if (rc)
2318                 GOTO(out, rc);
2319         lli->lli_synced_to_mds = false;
2320
2321         if (!S_ISREG(inode->i_mode) || hsm_import)
2322                 GOTO(out, rc = 0);
2323
2324         if (attr->ia_valid & (ATTR_SIZE | ATTR_ATIME | ATTR_ATIME_SET |
2325                               ATTR_MTIME | ATTR_MTIME_SET | ATTR_CTIME) ||
2326             xvalid & OP_XVALID_CTIME_SET) {
2327                 bool cached = false;
2328
2329                 rc = pcc_inode_setattr(inode, attr, &cached);
2330                 if (cached) {
2331                         if (rc) {
2332                                 CERROR("%s: PCC inode "DFID" setattr failed: "
2333                                        "rc = %d\n",
2334                                        ll_i2sbi(inode)->ll_fsname,
2335                                        PFID(&lli->lli_fid), rc);
2336                                 GOTO(out, rc);
2337                         }
2338                 } else {
2339                         unsigned int flags = 0;
2340
2341                         /* For truncate and utimes sending attributes to OSTs,
2342                          * setting mtime/atime to the past will be performed
2343                          * under PW [0:EOF] extent lock (new_size:EOF for
2344                          * truncate). It may seem excessive to send mtime/atime
2345                          * updates to OSTs when not setting times to past, but
2346                          * it is necessary due to possible time
2347                          * de-synchronization between MDT inode and OST objects
2348                          */
2349                         if (S_ISREG(inode->i_mode) && IS_ENCRYPTED(inode)) {
2350                                 xvalid |= OP_XVALID_FLAGS;
2351                                 flags = LUSTRE_ENCRYPT_FL;
2352                                 /* Call to ll_io_zero_page is not necessary if
2353                                  * truncating on PAGE_SIZE boundary, because
2354                                  * whole pages will be wiped.
2355                                  * In case of Direct IO, all we need is to set
2356                                  * new size.
2357                                  */
2358                                 if (attr->ia_valid & ATTR_SIZE &&
2359                                     attr->ia_size & ~PAGE_MASK &&
2360                                     !(attr->ia_valid & ATTR_FILE &&
2361                                       attr->ia_file->f_flags & O_DIRECT)) {
2362                                         pgoff_t offset =
2363                                                 attr->ia_size & (PAGE_SIZE - 1);
2364
2365                                         rc = ll_io_zero_page(inode,
2366                                                     attr->ia_size >> PAGE_SHIFT,
2367                                                     offset, PAGE_SIZE - offset);
2368                                         if (rc)
2369                                                 GOTO(out, rc);
2370                                 }
2371                                 /* If encrypted volatile file without the key,
2372                                  * we need to fetch size from reference file,
2373                                  * and set it on OST objects. This happens when
2374                                  * migrating or extending an encrypted file
2375                                  * without the key.
2376                                  */
2377                                 if (filename_is_volatile(dentry->d_name.name,
2378                                                          dentry->d_name.len,
2379                                                          NULL) &&
2380                                     llcrypt_require_key(inode) == -ENOKEY) {
2381                                         struct file *ref_file;
2382                                         struct inode *ref_inode;
2383                                         struct ll_inode_info *ref_lli;
2384                                         struct cl_object *ref_obj;
2385                                         struct cl_attr ref_attr = { 0 };
2386                                         struct lu_env *env;
2387                                         __u16 refcheck;
2388
2389                                         rc = volatile_ref_file(
2390                                                 dentry->d_name.name,
2391                                                 dentry->d_name.len,
2392                                                 &ref_file);
2393                                         if (rc)
2394                                                 GOTO(out, rc);
2395
2396                                         ref_inode = file_inode(ref_file);
2397                                         if (!ref_inode) {
2398                                                 fput(ref_file);
2399                                                 GOTO(out, rc = -EINVAL);
2400                                         }
2401
2402                                         env = cl_env_get(&refcheck);
2403                                         if (IS_ERR(env))
2404                                                 GOTO(out, rc = PTR_ERR(env));
2405
2406                                         ref_lli = ll_i2info(ref_inode);
2407                                         ref_obj = ref_lli->lli_clob;
2408                                         cl_object_attr_lock(ref_obj);
2409                                         rc = cl_object_attr_get(env, ref_obj,
2410                                                                 &ref_attr);
2411                                         cl_object_attr_unlock(ref_obj);
2412                                         cl_env_put(env, &refcheck);
2413                                         fput(ref_file);
2414                                         if (rc)
2415                                                 GOTO(out, rc);
2416
2417                                         attr->ia_valid |= ATTR_SIZE;
2418                                         attr->ia_size = ref_attr.cat_size;
2419                                 }
2420                         }
2421                         rc = cl_setattr_ost(lli->lli_clob, attr, xvalid, flags);
2422                 }
2423         }
2424
2425         /* If the file was restored, it needs to set dirty flag.
2426          *
2427          * We've already sent MDS_DATA_MODIFIED flag in
2428          * ll_md_setattr() for truncate. However, the MDT refuses to
2429          * set the HS_DIRTY flag on released files, so we have to set
2430          * it again if the file has been restored. Please check how
2431          * LLIF_DATA_MODIFIED is set in vvp_io_setattr_fini().
2432          *
2433          * Please notice that if the file is not released, the previous
2434          * MDS_DATA_MODIFIED has taken effect and usually
2435          * LLIF_DATA_MODIFIED is not set(see vvp_io_setattr_fini()).
2436          * This way we can save an RPC for common open + trunc
2437          * operation. */
2438         if (test_and_clear_bit(LLIF_DATA_MODIFIED, &lli->lli_flags)) {
2439                 struct hsm_state_set hss = {
2440                         .hss_valid = HSS_SETMASK,
2441                         .hss_setmask = HS_DIRTY,
2442                 };
2443                 int rc2;
2444
2445                 rc2 = ll_hsm_state_set(inode, &hss);
2446                 /* truncate and write can happen at the same time, so that
2447                  * the file can be set modified even though the file is not
2448                  * restored from released state, and ll_hsm_state_set() is
2449                  * not applicable for the file, and rc2 < 0 is normal in this
2450                  * case. */
2451                 if (rc2 < 0)
2452                         CDEBUG(D_INFO, DFID "HSM set dirty failed: rc2 = %d\n",
2453                                PFID(ll_inode2fid(inode)), rc2);
2454         }
2455
2456         EXIT;
2457 out:
2458         if (op_data != NULL)
2459                 ll_finish_md_op_data(op_data);
2460
2461         if (S_ISREG(inode->i_mode)) {
2462                 ll_inode_lock(inode);
2463                 if ((attr->ia_valid & ATTR_SIZE) && !hsm_import)
2464                         inode_dio_wait(inode);
2465                 /* Once we've got the i_mutex, it's safe to set the S_NOSEC
2466                  * flag.  ll_update_inode (called from ll_md_setattr), clears
2467                  * inode flags, so there is a gap where S_NOSEC is not set.
2468                  * This can cause a writer to take the i_mutex unnecessarily,
2469                  * but this is safe to do and should be rare. */
2470                 inode_has_no_xattr(inode);
2471         }
2472
2473         if (!rc)
2474                 ll_stats_ops_tally(ll_i2sbi(inode), attr->ia_valid & ATTR_SIZE ?
2475                                         LPROC_LL_TRUNC : LPROC_LL_SETATTR,
2476                                    ktime_us_delta(ktime_get(), kstart));
2477 clear:
2478         ll_clear_inode_lock_owner(inode);
2479
2480         RETURN(rc);
2481 }
2482
2483 int ll_setattr(struct user_namespace *mnt_userns, struct dentry *de,
2484                struct iattr *attr)
2485 {
2486         int mode = de->d_inode->i_mode;
2487         enum op_xvalid xvalid = 0;
2488         int rc;
2489
2490         rc = llcrypt_prepare_setattr(de, attr);
2491         if (rc)
2492                 return rc;
2493
2494         if ((attr->ia_valid & (ATTR_CTIME|ATTR_SIZE|ATTR_MODE)) ==
2495                               (ATTR_CTIME|ATTR_SIZE|ATTR_MODE))
2496                 xvalid |= OP_XVALID_OWNEROVERRIDE;
2497
2498         if (((attr->ia_valid & (ATTR_MODE|ATTR_FORCE|ATTR_SIZE)) ==
2499                                (ATTR_SIZE|ATTR_MODE)) &&
2500             (((mode & S_ISUID) && !(attr->ia_mode & S_ISUID)) ||
2501              (((mode & (S_ISGID|S_IXGRP)) == (S_ISGID|S_IXGRP)) &&
2502               !(attr->ia_mode & S_ISGID))))
2503                 attr->ia_valid |= ATTR_FORCE;
2504
2505         if ((attr->ia_valid & ATTR_MODE) &&
2506             (mode & S_ISUID) &&
2507             !(attr->ia_mode & S_ISUID) &&
2508             !(attr->ia_valid & ATTR_KILL_SUID))
2509                 attr->ia_valid |= ATTR_KILL_SUID;
2510
2511         if ((attr->ia_valid & ATTR_MODE) &&
2512             ((mode & (S_ISGID|S_IXGRP)) == (S_ISGID|S_IXGRP)) &&
2513             !(attr->ia_mode & S_ISGID) &&
2514             !(attr->ia_valid & ATTR_KILL_SGID))
2515                 attr->ia_valid |= ATTR_KILL_SGID;
2516
2517         return ll_setattr_raw(de, attr, xvalid, false);
2518 }
2519
2520 int ll_statfs_internal(struct ll_sb_info *sbi, struct obd_statfs *osfs,
2521                        u32 flags)
2522 {
2523         struct obd_statfs obd_osfs = { 0 };
2524         time64_t max_age;
2525         int rc;
2526
2527         ENTRY;
2528         max_age = ktime_get_seconds() - sbi->ll_statfs_max_age;
2529
2530         if (test_bit(LL_SBI_LAZYSTATFS, sbi->ll_flags))
2531                 flags |= OBD_STATFS_NODELAY;
2532
2533         rc = obd_statfs(NULL, sbi->ll_md_exp, osfs, max_age, flags);
2534         if (rc)
2535                 RETURN(rc);
2536
2537         osfs->os_type = LL_SUPER_MAGIC;
2538
2539         CDEBUG(D_SUPER, "MDC blocks %llu/%llu objects %llu/%llu\n",
2540               osfs->os_bavail, osfs->os_blocks, osfs->os_ffree, osfs->os_files);
2541
2542         if (osfs->os_state & OS_STATFS_SUM)
2543                 GOTO(out, rc);
2544
2545         rc = obd_statfs(NULL, sbi->ll_dt_exp, &obd_osfs, max_age, flags);
2546         if (rc) /* Possibly a filesystem with no OSTs.  Report MDT totals. */
2547                 GOTO(out, rc = 0);
2548
2549         CDEBUG(D_SUPER, "OSC blocks %llu/%llu objects %llu/%llu\n",
2550                obd_osfs.os_bavail, obd_osfs.os_blocks, obd_osfs.os_ffree,
2551                obd_osfs.os_files);
2552
2553         osfs->os_bsize = obd_osfs.os_bsize;
2554         osfs->os_blocks = obd_osfs.os_blocks;
2555         osfs->os_bfree = obd_osfs.os_bfree;
2556         osfs->os_bavail = obd_osfs.os_bavail;
2557
2558         /* If we have _some_ OSTs, but don't have as many free objects on the
2559          * OSTs as inodes on the MDTs, reduce the reported number of inodes
2560          * to compensate, so that the "inodes in use" number is correct.
2561          * This should be kept in sync with lod_statfs() behaviour.
2562          */
2563         if (obd_osfs.os_files && obd_osfs.os_ffree < osfs->os_ffree) {
2564                 osfs->os_files = (osfs->os_files - osfs->os_ffree) +
2565                                  obd_osfs.os_ffree;
2566                 osfs->os_ffree = obd_osfs.os_ffree;
2567         }
2568
2569 out:
2570         RETURN(rc);
2571 }
2572
2573 static int ll_statfs_project(struct inode *inode, struct kstatfs *sfs)
2574 {
2575         struct if_quotactl qctl = {
2576                 .qc_cmd = LUSTRE_Q_GETQUOTA,
2577                 .qc_type = PRJQUOTA,
2578                 .qc_valid = QC_GENERAL,
2579         };
2580         u64 limit, curblock;
2581         int ret;
2582
2583         qctl.qc_id = ll_i2info(inode)->lli_projid;
2584         ret = quotactl_ioctl(inode->i_sb, &qctl);
2585         if (ret) {
2586                 /* ignore errors if project ID does not have
2587                  * a quota limit or feature unsupported.
2588                  */
2589                 if (ret == -ESRCH || ret == -EOPNOTSUPP)
2590                         ret = 0;
2591                 return ret;
2592         }
2593
2594         limit = ((qctl.qc_dqblk.dqb_bsoftlimit ?
2595                  qctl.qc_dqblk.dqb_bsoftlimit :
2596                  qctl.qc_dqblk.dqb_bhardlimit) * 1024) / sfs->f_bsize;
2597         if (limit && sfs->f_blocks > limit) {
2598                 curblock = (qctl.qc_dqblk.dqb_curspace +
2599                                 sfs->f_bsize - 1) / sfs->f_bsize;
2600                 sfs->f_blocks = limit;
2601                 sfs->f_bfree = sfs->f_bavail =
2602                         (sfs->f_blocks > curblock) ?
2603                         (sfs->f_blocks - curblock) : 0;
2604         }
2605
2606         limit = qctl.qc_dqblk.dqb_isoftlimit ?
2607                 qctl.qc_dqblk.dqb_isoftlimit :
2608                 qctl.qc_dqblk.dqb_ihardlimit;
2609         if (limit && sfs->f_files > limit) {
2610                 sfs->f_files = limit;
2611                 sfs->f_ffree = (sfs->f_files >
2612                         qctl.qc_dqblk.dqb_curinodes) ?
2613                         (sfs->f_files - qctl.qc_dqblk.dqb_curinodes) : 0;
2614         }
2615
2616         return 0;
2617 }
2618
2619 int ll_statfs(struct dentry *de, struct kstatfs *sfs)
2620 {
2621         struct super_block *sb = de->d_sb;
2622         struct obd_statfs osfs;
2623         __u64 fsid = huge_encode_dev(sb->s_dev);
2624         ktime_t kstart = ktime_get();
2625         int rc;
2626
2627         CDEBUG(D_VFSTRACE, "VFS Op:sb=%s (%p)\n", sb->s_id, sb);
2628
2629         /* Some amount of caching on the client is allowed */
2630         rc = ll_statfs_internal(ll_s2sbi(sb), &osfs, OBD_STATFS_SUM);
2631         if (rc)
2632                 return rc;
2633
2634         statfs_unpack(sfs, &osfs);
2635
2636         /* We need to downshift for all 32-bit kernels, because we can't
2637          * tell if the kernel is being called via sys_statfs64() or not.
2638          * Stop before overflowing f_bsize - in which case it is better
2639          * to just risk EOVERFLOW if caller is using old sys_statfs(). */
2640         if (sizeof(long) < 8) {
2641                 while (osfs.os_blocks > ~0UL && sfs->f_bsize < 0x40000000) {
2642                         sfs->f_bsize <<= 1;
2643
2644                         osfs.os_blocks >>= 1;
2645                         osfs.os_bfree >>= 1;
2646                         osfs.os_bavail >>= 1;
2647                 }
2648         }
2649
2650         sfs->f_blocks = osfs.os_blocks;
2651         sfs->f_bfree = osfs.os_bfree;
2652         sfs->f_bavail = osfs.os_bavail;
2653         sfs->f_fsid.val[0] = (__u32)fsid;
2654         sfs->f_fsid.val[1] = (__u32)(fsid >> 32);
2655         if (ll_i2info(de->d_inode)->lli_projid &&
2656             test_bit(LLIF_PROJECT_INHERIT, &ll_i2info(de->d_inode)->lli_flags))
2657                 return ll_statfs_project(de->d_inode, sfs);
2658
2659         ll_stats_ops_tally(ll_s2sbi(sb), LPROC_LL_STATFS,
2660                            ktime_us_delta(ktime_get(), kstart));
2661
2662         return 0;
2663 }
2664
2665 void ll_inode_size_lock(struct inode *inode)
2666 {
2667         struct ll_inode_info *lli;
2668
2669         LASSERT(!S_ISDIR(inode->i_mode));
2670
2671         lli = ll_i2info(inode);
2672         mutex_lock(&lli->lli_size_mutex);
2673         lli->lli_size_lock_owner = current;
2674 }
2675
2676 void ll_inode_size_unlock(struct inode *inode)
2677 {
2678         struct ll_inode_info *lli;
2679
2680         lli = ll_i2info(inode);
2681         lli->lli_size_lock_owner = NULL;
2682         mutex_unlock(&lli->lli_size_mutex);
2683 }
2684
2685 int ll_inode_size_trylock(struct inode *inode)
2686 {
2687         struct ll_inode_info *lli;
2688
2689         LASSERT(!S_ISDIR(inode->i_mode));
2690
2691         lli = ll_i2info(inode);
2692         return mutex_trylock(&lli->lli_size_mutex);
2693 }
2694
2695 void ll_update_inode_flags(struct inode *inode, unsigned int ext_flags)
2696 {
2697         /* do not clear encryption flag */
2698         ext_flags |= ll_inode_to_ext_flags(inode->i_flags) & LUSTRE_ENCRYPT_FL;
2699         inode->i_flags = ll_ext_to_inode_flags(ext_flags);
2700         if (ext_flags & LUSTRE_PROJINHERIT_FL)
2701                 set_bit(LLIF_PROJECT_INHERIT, &ll_i2info(inode)->lli_flags);
2702         else
2703                 clear_bit(LLIF_PROJECT_INHERIT, &ll_i2info(inode)->lli_flags);
2704 }
2705
2706 int ll_update_inode(struct inode *inode, struct lustre_md *md)
2707 {
2708         struct ll_inode_info *lli = ll_i2info(inode);
2709         struct mdt_body *body = md->body;
2710         struct ll_sb_info *sbi = ll_i2sbi(inode);
2711         bool api32;
2712         int rc = 0;
2713
2714         if (body->mbo_valid & OBD_MD_FLEASIZE) {
2715                 rc = cl_file_inode_init(inode, md);
2716                 if (rc)
2717                         return rc;
2718         }
2719
2720         if (S_ISDIR(inode->i_mode)) {
2721                 rc = ll_update_lsm_md(inode, md);
2722                 if (rc != 0)
2723                         return rc;
2724         }
2725
2726         if (body->mbo_valid & OBD_MD_FLACL)
2727                 lli_replace_acl(lli, md);
2728
2729         api32 = test_bit(LL_SBI_32BIT_API, sbi->ll_flags);
2730         inode->i_ino = cl_fid_build_ino(&body->mbo_fid1, api32);
2731         inode->i_generation = cl_fid_build_gen(&body->mbo_fid1);
2732
2733         if (body->mbo_valid & OBD_MD_FLATIME) {
2734                 if (body->mbo_atime > inode->i_atime.tv_sec)
2735                         inode->i_atime.tv_sec = body->mbo_atime;
2736                 lli->lli_atime = body->mbo_atime;
2737         }
2738
2739         if (body->mbo_valid & OBD_MD_FLMTIME) {
2740                 if (body->mbo_mtime > inode->i_mtime.tv_sec) {
2741                         CDEBUG(D_INODE,
2742                                "setting ino %lu mtime from %lld to %llu\n",
2743                                inode->i_ino, (s64)inode->i_mtime.tv_sec,
2744                                body->mbo_mtime);
2745                         inode->i_mtime.tv_sec = body->mbo_mtime;
2746                 }
2747                 lli->lli_mtime = body->mbo_mtime;
2748         }
2749
2750         if (body->mbo_valid & OBD_MD_FLCTIME) {
2751                 if (body->mbo_ctime > inode->i_ctime.tv_sec)
2752                         inode->i_ctime.tv_sec = body->mbo_ctime;
2753                 lli->lli_ctime = body->mbo_ctime;
2754         }
2755
2756         if (body->mbo_valid & OBD_MD_FLBTIME)
2757                 lli->lli_btime = body->mbo_btime;
2758
2759         /* Clear i_flags to remove S_NOSEC before permissions are updated */
2760         if (body->mbo_valid & OBD_MD_FLFLAGS)
2761                 ll_update_inode_flags(inode, body->mbo_flags);
2762         if (body->mbo_valid & OBD_MD_FLMODE)
2763                 inode->i_mode = (inode->i_mode & S_IFMT) |
2764                                 (body->mbo_mode & ~S_IFMT);
2765
2766         if (body->mbo_valid & OBD_MD_FLTYPE)
2767                 inode->i_mode = (inode->i_mode & ~S_IFMT) |
2768                                 (body->mbo_mode & S_IFMT);
2769
2770         LASSERT(inode->i_mode != 0);
2771         if (body->mbo_valid & OBD_MD_FLUID)
2772                 inode->i_uid = make_kuid(&init_user_ns, body->mbo_uid);
2773         if (body->mbo_valid & OBD_MD_FLGID)
2774                 inode->i_gid = make_kgid(&init_user_ns, body->mbo_gid);
2775         if (body->mbo_valid & OBD_MD_FLPROJID)
2776                 lli->lli_projid = body->mbo_projid;
2777         if (body->mbo_valid & OBD_MD_FLNLINK) {
2778                 spin_lock(&inode->i_lock);
2779                 set_nlink(inode, body->mbo_nlink);
2780                 spin_unlock(&inode->i_lock);
2781         }
2782         if (body->mbo_valid & OBD_MD_FLRDEV)
2783                 inode->i_rdev = old_decode_dev(body->mbo_rdev);
2784
2785         if (body->mbo_valid & OBD_MD_FLID) {
2786                 /* FID shouldn't be changed! */
2787                 if (fid_is_sane(&lli->lli_fid)) {
2788                         LASSERTF(lu_fid_eq(&lli->lli_fid, &body->mbo_fid1),
2789                                  "Trying to change FID "DFID
2790                                  " to the "DFID", inode "DFID"(%p)\n",
2791                                  PFID(&lli->lli_fid), PFID(&body->mbo_fid1),
2792                                  PFID(ll_inode2fid(inode)), inode);
2793                 } else {
2794                         lli->lli_fid = body->mbo_fid1;
2795                 }
2796         }
2797
2798         LASSERT(fid_seq(&lli->lli_fid) != 0);
2799
2800         /* In case of encrypted file without the key, please do not lose
2801          * clear text size stored into lli_lazysize in ll_merge_attr(),
2802          * we will need it in ll_prepare_close().
2803          */
2804         if (lli->lli_attr_valid & OBD_MD_FLLAZYSIZE && lli->lli_lazysize &&
2805             llcrypt_require_key(inode) == -ENOKEY)
2806                 lli->lli_attr_valid = body->mbo_valid | OBD_MD_FLLAZYSIZE;
2807         else
2808                 lli->lli_attr_valid = body->mbo_valid;
2809         if (body->mbo_valid & OBD_MD_FLSIZE) {
2810                 i_size_write(inode, body->mbo_size);
2811
2812                 CDEBUG(D_VFSTRACE, "inode="DFID", updating i_size %llu\n",
2813                        PFID(ll_inode2fid(inode)),
2814                        (unsigned long long)body->mbo_size);
2815
2816                 if (body->mbo_valid & OBD_MD_FLBLOCKS)
2817                         inode->i_blocks = body->mbo_blocks;
2818         } else {
2819                 if (body->mbo_valid & OBD_MD_FLLAZYSIZE)
2820                         lli->lli_lazysize = body->mbo_size;
2821                 if (body->mbo_valid & OBD_MD_FLLAZYBLOCKS)
2822                         lli->lli_lazyblocks = body->mbo_blocks;
2823         }
2824
2825         if (body->mbo_valid & OBD_MD_TSTATE) {
2826                 /* Set LLIF_FILE_RESTORING if restore ongoing and
2827                  * clear it when done to ensure to start again
2828                  * glimpsing updated attrs
2829                  */
2830                 if (body->mbo_t_state & MS_RESTORE)
2831                         set_bit(LLIF_FILE_RESTORING, &lli->lli_flags);
2832                 else
2833                         clear_bit(LLIF_FILE_RESTORING, &lli->lli_flags);
2834         }
2835
2836         return 0;
2837 }
2838
2839 /* child default LMV is inherited from parent */
2840 static inline bool ll_default_lmv_inherited(struct lmv_stripe_md *pdmv,
2841                                             struct lmv_stripe_md *cdmv)
2842 {
2843         if (!pdmv || !cdmv)
2844                 return false;
2845
2846         if (pdmv->lsm_md_magic != cdmv->lsm_md_magic ||
2847             pdmv->lsm_md_stripe_count != cdmv->lsm_md_stripe_count ||
2848             pdmv->lsm_md_master_mdt_index != cdmv->lsm_md_master_mdt_index ||
2849             pdmv->lsm_md_hash_type != cdmv->lsm_md_hash_type)
2850                 return false;
2851
2852         if (cdmv->lsm_md_max_inherit !=
2853             lmv_inherit_next(pdmv->lsm_md_max_inherit))
2854                 return false;
2855
2856         if (cdmv->lsm_md_max_inherit_rr !=
2857             lmv_inherit_rr_next(pdmv->lsm_md_max_inherit_rr))
2858                 return false;
2859
2860         return true;
2861 }
2862
2863 /* if default LMV is implicitly inherited, subdir default LMV is maintained on
2864  * client side.
2865  */
2866 static int ll_dir_default_lmv_inherit(struct inode *dir, struct inode *inode)
2867 {
2868         struct ll_inode_info *plli = ll_i2info(dir);
2869         struct ll_inode_info *lli = ll_i2info(inode);
2870         struct lmv_stripe_md *plsm;
2871         struct lmv_stripe_md *lsm;
2872         int rc = 0;
2873
2874         ENTRY;
2875
2876         /* ROOT default LMV is not inherited */
2877         if (is_root_inode(dir) ||
2878             !(exp_connect_flags2(ll_i2mdexp(dir)) &
2879                                  OBD_CONNECT2_DMV_IMP_INHERIT))
2880                 RETURN(0);
2881
2882         /* nothing to do if no default LMV on both */
2883         if (!plli->lli_default_lsm_md && !lli->lli_default_lsm_md)
2884                 RETURN(0);
2885
2886         /* subdir default LMV comes from disk */
2887         if (lli->lli_default_lsm_md && lli->lli_default_lmv_set)
2888                 RETURN(0);
2889
2890         /* delete subdir default LMV if parent's is deleted or becomes
2891          * uninheritable.
2892          */
2893         down_read(&plli->lli_lsm_sem);
2894         plsm = plli->lli_default_lsm_md;
2895         if (!plsm || !lmv_is_inheritable(plsm->lsm_md_max_inherit)) {
2896                 if (lli->lli_default_lsm_md && !lli->lli_default_lmv_set) {
2897                         down_write(&lli->lli_lsm_sem);
2898                         if (lli->lli_default_lsm_md &&
2899                             !lli->lli_default_lmv_set) {
2900                                 lmv_free_memmd(lli->lli_default_lsm_md);
2901                                 lli->lli_default_lsm_md = NULL;
2902                                 lli->lli_inherit_depth = 0;
2903                         }
2904                         up_write(&lli->lli_lsm_sem);
2905                 }
2906                 GOTO(unlock_parent, rc = 0);
2907         }
2908
2909         /* do nothing if inherited LMV is unchanged */
2910         if (lli->lli_default_lsm_md) {
2911                 rc = 1;
2912                 down_read(&lli->lli_lsm_sem);
2913                 if (!lli->lli_default_lmv_set)
2914                         rc = lsm_md_inherited(plsm, lli->lli_default_lsm_md);
2915                 up_read(&lli->lli_lsm_sem);
2916                 if (rc == 1)
2917                         GOTO(unlock_parent, rc = 0);
2918         }
2919
2920         /* inherit default LMV */
2921         down_write(&lli->lli_lsm_sem);
2922         if (lli->lli_default_lsm_md) {
2923                 /* checked above, but in case of race, check again with lock */
2924                 if (lli->lli_default_lmv_set)
2925                         GOTO(unlock_child, rc = 0);
2926                 /* always update subdir default LMV in case parent's changed */
2927                 lsm = lli->lli_default_lsm_md;
2928         } else {
2929                 OBD_ALLOC_PTR(lsm);
2930                 if (!lsm)
2931                         GOTO(unlock_child, rc = -ENOMEM);
2932                 lli->lli_default_lsm_md = lsm;
2933         }
2934
2935         *lsm = *plsm;
2936         lsm->lsm_md_max_inherit = lmv_inherit_next(plsm->lsm_md_max_inherit);
2937         lsm->lsm_md_max_inherit_rr =
2938                         lmv_inherit_rr_next(plsm->lsm_md_max_inherit_rr);
2939         lli->lli_inherit_depth = plli->lli_inherit_depth + 1;
2940
2941         lsm_md_dump(D_INODE, lsm);
2942
2943         EXIT;
2944 unlock_child:
2945         up_write(&lli->lli_lsm_sem);
2946 unlock_parent:
2947         up_read(&plli->lli_lsm_sem);
2948
2949         return rc;
2950 }
2951
2952 enum lsm_sem_class {
2953         LSM_SEM_PARENT,
2954         LSM_SEM_CHILD,
2955 };
2956
2957 /**
2958  * Update directory depth and default LMV
2959  *
2960  * Update directory depth to ROOT and inherit default LMV from parent if
2961  * parent's default LMV is inheritable. The default LMV set with command
2962  * "lfs setdirstripe -D ..." is stored on MDT, while the inherited default LMV
2963  * is generated at runtime on client side.
2964  *
2965  * \param[in]   dir     parent directory inode
2966  * \param[in]   de      dentry
2967  */
2968 void ll_update_dir_depth_dmv(struct inode *dir, struct dentry *de)
2969 {
2970         struct inode *inode = de->d_inode;
2971         struct ll_inode_info *plli;
2972         struct ll_inode_info *lli;
2973
2974         LASSERT(S_ISDIR(inode->i_mode));
2975         if (inode == dir)
2976                 return;
2977
2978         plli = ll_i2info(dir);
2979         lli = ll_i2info(inode);
2980         lli->lli_dir_depth = plli->lli_dir_depth + 1;
2981         if (lli->lli_default_lsm_md && lli->lli_default_lmv_set) {
2982                 if (plli->lli_default_lsm_md) {
2983                         down_read_nested(&plli->lli_lsm_sem, LSM_SEM_PARENT);
2984                         down_read_nested(&lli->lli_lsm_sem, LSM_SEM_CHILD);
2985                         if (lsm_md_inherited(plli->lli_default_lsm_md,
2986                                              lli->lli_default_lsm_md))
2987                                 lli->lli_inherit_depth =
2988                                         plli->lli_inherit_depth + 1;
2989                         else
2990                                 /* in case parent default LMV changed */
2991                                 lli->lli_inherit_depth = 0;
2992                         up_read(&lli->lli_lsm_sem);
2993                         up_read(&plli->lli_lsm_sem);
2994                 } else {
2995                         /* in case parent default LMV deleted */
2996                         lli->lli_inherit_depth = 0;
2997                 }
2998         } else {
2999                 ll_dir_default_lmv_inherit(dir, inode);
3000         }
3001
3002         if (lli->lli_default_lsm_md)
3003                 CDEBUG(D_INODE,
3004                        "%s "DFID" depth %hu %s default LMV inherit depth %hu\n",
3005                        de->d_name.name, PFID(&lli->lli_fid), lli->lli_dir_depth,
3006                        lli->lli_default_lmv_set ? "server" : "client",
3007                        lli->lli_inherit_depth);
3008 }
3009
3010 void ll_truncate_inode_pages_final(struct inode *inode)
3011 {
3012         struct address_space *mapping = &inode->i_data;
3013         unsigned long nrpages;
3014         unsigned long flags;
3015
3016         LASSERTF((inode->i_state & I_FREEING) || inode_is_locked(inode),
3017                  DFID ":inode %p state %#lx, lli_flags %#lx\n",
3018                  PFID(ll_inode2fid(inode)), inode, inode->i_state,
3019                  ll_i2info(inode)->lli_flags);
3020
3021         truncate_inode_pages_final(mapping);
3022
3023         /* Workaround for LU-118: Note nrpages may not be totally updated when
3024          * truncate_inode_pages() returns, as there can be a page in the process
3025          * of deletion (inside __delete_from_page_cache()) in the specified
3026          * range. Thus mapping->nrpages can be non-zero when this function
3027          * returns even after truncation of the whole mapping.  Only do this if
3028          * npages isn't already zero.
3029          */
3030         nrpages = mapping->nrpages;
3031         if (nrpages) {
3032                 ll_xa_lock_irqsave(&mapping->i_pages, flags);
3033                 nrpages = mapping->nrpages;
3034                 ll_xa_unlock_irqrestore(&mapping->i_pages, flags);
3035         } /* Workaround end */
3036
3037         LASSERTF(nrpages == 0, "%s: inode="DFID"(%p) nrpages=%lu "
3038                  "state %#lx, lli_flags %#lx, "
3039                  "see https://jira.whamcloud.com/browse/LU-118\n",
3040                  ll_i2sbi(inode)->ll_fsname,
3041                  PFID(ll_inode2fid(inode)), inode, nrpages,
3042                  inode->i_state, ll_i2info(inode)->lli_flags);
3043 }
3044
3045 int ll_read_inode2(struct inode *inode, void *opaque)
3046 {
3047         struct lustre_md *md = opaque;
3048         struct ll_inode_info *lli = ll_i2info(inode);
3049         int     rc;
3050         ENTRY;
3051
3052         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
3053                PFID(&lli->lli_fid), inode);
3054
3055         /* Core attributes from the MDS first.  This is a new inode, and
3056          * the VFS doesn't zero times in the core inode so we have to do
3057          * it ourselves.  They will be overwritten by either MDS or OST
3058          * attributes - we just need to make sure they aren't newer.
3059          */
3060         inode->i_mtime.tv_sec = 0;
3061         inode->i_atime.tv_sec = 0;
3062         inode->i_ctime.tv_sec = 0;
3063         inode->i_rdev = 0;
3064         rc = ll_update_inode(inode, md);
3065         if (rc != 0)
3066                 RETURN(rc);
3067
3068         /* OIDEBUG(inode); */
3069
3070 #ifdef HAVE_BACKING_DEV_INFO
3071         /* initializing backing dev info. */
3072         inode->i_mapping->backing_dev_info = &s2lsi(inode->i_sb)->lsi_bdi;
3073 #endif
3074         if (S_ISREG(inode->i_mode)) {
3075                 struct ll_sb_info *sbi = ll_i2sbi(inode);
3076                 inode->i_op = &ll_file_inode_operations;
3077                 inode->i_fop = sbi->ll_fop;
3078                 inode->i_mapping->a_ops = (struct address_space_operations *)&ll_aops;
3079                 EXIT;
3080         } else if (S_ISDIR(inode->i_mode)) {
3081                 inode->i_op = &ll_dir_inode_operations;
3082                 inode->i_fop = &ll_dir_operations;
3083                 EXIT;
3084         } else if (S_ISLNK(inode->i_mode)) {
3085                 inode->i_op = &ll_fast_symlink_inode_operations;
3086                 EXIT;
3087         } else {
3088                 inode->i_op = &ll_special_inode_operations;
3089
3090                 init_special_inode(inode, inode->i_mode,
3091                                    inode->i_rdev);
3092
3093                 EXIT;
3094         }
3095
3096         return 0;
3097 }
3098
3099 void ll_delete_inode(struct inode *inode)
3100 {
3101         struct ll_inode_info *lli = ll_i2info(inode);
3102         ENTRY;
3103
3104         if (S_ISREG(inode->i_mode) && lli->lli_clob != NULL) {
3105                 /* It is last chance to write out dirty pages,
3106                  * otherwise we may lose data while umount.
3107                  *
3108                  * If i_nlink is 0 then just discard data. This is safe because
3109                  * local inode gets i_nlink 0 from server only for the last
3110                  * unlink, so that file is not opened somewhere else
3111                  */
3112                 cl_sync_file_range(inode, 0, OBD_OBJECT_EOF, inode->i_nlink ?
3113                                    CL_FSYNC_LOCAL : CL_FSYNC_DISCARD, 1);
3114         }
3115
3116         ll_truncate_inode_pages_final(inode);
3117         ll_clear_inode(inode);
3118         clear_inode(inode);
3119
3120         EXIT;
3121 }
3122
3123 static int fileattr_get(struct inode *inode, int *flags,
3124                         u32 *xflags,
3125                         u32 *projid)
3126 {
3127         struct ll_sb_info *sbi = ll_i2sbi(inode);
3128         struct ptlrpc_request *req = NULL;
3129         struct md_op_data *op_data;
3130         struct mdt_body *body;
3131         int rc;
3132
3133         ENTRY;
3134         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL,
3135                                      0, 0, LUSTRE_OPC_ANY,
3136                                      NULL);
3137         if (IS_ERR(op_data))
3138                 RETURN(PTR_ERR(op_data));
3139
3140         op_data->op_valid = OBD_MD_FLFLAGS;
3141         rc = md_getattr(sbi->ll_md_exp, op_data, &req);
3142         ll_finish_md_op_data(op_data);
3143         if (rc) {
3144                 CERROR("%s: failure inode "DFID": rc = %d\n",
3145                        sbi->ll_md_exp->exp_obd->obd_name,
3146                        PFID(ll_inode2fid(inode)), rc);
3147                 RETURN(-abs(rc));
3148         }
3149
3150         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
3151
3152         *flags = body->mbo_flags;
3153         /* if Lustre specific LUSTRE_ENCRYPT_FL flag is set, also set
3154          * ext4 equivalent to please lsattr and other e2fsprogs tools
3155          */
3156         if (*flags & LUSTRE_ENCRYPT_FL)
3157                 *flags |= STATX_ATTR_ENCRYPTED;
3158
3159         ptlrpc_req_finished(req);
3160
3161         *xflags = ll_inode_flags_to_xflags(inode->i_flags);
3162         if (test_bit(LLIF_PROJECT_INHERIT, &ll_i2info(inode)->lli_flags))
3163                 *xflags |= FS_XFLAG_PROJINHERIT;
3164         *projid = ll_i2info(inode)->lli_projid;
3165
3166         RETURN(0);
3167 }
3168
3169 static int fileattr_set(struct inode *inode, int flags)
3170 {
3171         struct ll_sb_info *sbi = ll_i2sbi(inode);
3172         struct ptlrpc_request *req = NULL;
3173         struct md_op_data *op_data;
3174         struct cl_object *obj;
3175         struct fsxattr fa = { 0 };
3176         struct iattr *attr;
3177         int rc;
3178
3179         ENTRY;
3180         fa.fsx_projid = ll_i2info(inode)->lli_projid;
3181         if (flags & LUSTRE_PROJINHERIT_FL)
3182                 fa.fsx_xflags = FS_XFLAG_PROJINHERIT;
3183
3184         rc = ll_ioctl_check_project(inode, fa.fsx_xflags,
3185                                     fa.fsx_projid);
3186         if (rc)
3187                 RETURN(rc);
3188
3189         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
3190                                      LUSTRE_OPC_ANY, NULL);
3191         if (IS_ERR(op_data))
3192                 RETURN(PTR_ERR(op_data));
3193
3194         op_data->op_attr_flags = flags;
3195         op_data->op_xvalid |= OP_XVALID_FLAGS;
3196         rc = md_setattr(sbi->ll_md_exp, op_data, NULL, 0, &req);
3197         ll_finish_md_op_data(op_data);
3198         ptlrpc_req_finished(req);
3199         if (rc)
3200                 RETURN(rc);
3201
3202         ll_update_inode_flags(inode, flags);
3203
3204         obj = ll_i2info(inode)->lli_clob;
3205         if (obj == NULL)
3206                 RETURN(0);
3207
3208         OBD_ALLOC_PTR(attr);
3209         if (attr == NULL)
3210                 RETURN(-ENOMEM);
3211
3212         rc = cl_setattr_ost(obj, attr, OP_XVALID_FLAGS, flags);
3213
3214         OBD_FREE_PTR(attr);
3215         RETURN(rc);
3216 }
3217
3218 #ifdef HAVE_FILEATTR_GET
3219 int ll_fileattr_get(struct dentry *dentry, struct fileattr *fa)
3220 {
3221         return fileattr_get(d_inode(dentry), &fa->flags,
3222                             &fa->fsx_xflags, &fa->fsx_projid);
3223 }
3224
3225 int ll_fileattr_set(struct user_namespace *mnt_userns,
3226                     struct dentry *dentry, struct fileattr *fa)
3227 {
3228         if (fa->fsx_valid)
3229                 return ll_set_project(d_inode(dentry), fa->fsx_xflags,
3230                                       fa->fsx_projid);
3231         else
3232                 return fileattr_set(d_inode(dentry), fa->flags);
3233 }
3234 #endif /* HAVE_FILEATTR_GET */
3235
3236 /* ioctl commands shared between files and directories */
3237 int ll_iocontrol(struct inode *inode, struct file *file,
3238                  unsigned int cmd, void __user *uarg)
3239 {
3240         int rc;
3241         ENTRY;
3242
3243         switch (cmd) {
3244         case BLKSSZGET:
3245                 RETURN(put_user(PAGE_SIZE, (int __user *)uarg));
3246         case LL_IOC_GETVERSION:
3247         case FS_IOC_GETVERSION:
3248                 RETURN(put_user(inode->i_generation, (int __user *)uarg));
3249
3250 #ifndef HAVE_FILEATTR_GET
3251         case FS_IOC_GETFLAGS: {
3252                 u32 xflags = 0, projid = 0;
3253                 int flags = 0;
3254
3255                 if (!ll_access_ok(uarg, sizeof(int)))
3256                         RETURN(-EFAULT);
3257                 rc = fileattr_get(file->f_inode, &flags, &xflags, &projid);
3258                 if (rc)
3259                         RETURN(rc);
3260
3261                 RETURN(put_user(flags, (int __user *)uarg));
3262         }
3263         case FS_IOC_SETFLAGS: {
3264                 int flags = 0;
3265
3266                 if (get_user(flags, (int __user *)uarg))
3267                         RETURN(-EFAULT);
3268
3269                 RETURN(fileattr_set(file->f_inode, flags));
3270         }
3271         case FS_IOC_FSGETXATTR:
3272                 RETURN(ll_ioctl_fsgetxattr(inode, cmd, uarg));
3273         case FS_IOC_FSSETXATTR:
3274                 RETURN(ll_ioctl_fssetxattr(inode, cmd, uarg));
3275 #endif /* HAVE_FILEATTR_GET */
3276         case LL_IOC_PROJECT:
3277                 RETURN(ll_ioctl_project(file, cmd, uarg));
3278         case IOC_OBD_STATFS:
3279                 RETURN(ll_obd_statfs(inode, uarg));
3280         case LL_IOC_GET_MDTIDX: {
3281                 if (!ll_access_ok(uarg, sizeof(rc)))
3282                         RETURN(-EFAULT);
3283
3284                 rc = ll_get_mdt_idx(inode);
3285                 if (rc < 0)
3286                         RETURN(rc);
3287
3288                 if (put_user(rc, (int __user *)uarg))
3289                         RETURN(-EFAULT);
3290
3291                 RETURN(0);
3292         }
3293         case LL_IOC_FLUSHCTX:
3294                 RETURN(ll_flush_ctx(inode));
3295 #ifdef HAVE_LUSTRE_CRYPTO
3296         case LL_IOC_ADD_ENCRYPTION_KEY:
3297                 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
3298                         return -EOPNOTSUPP;
3299                 rc = llcrypt_ioctl_add_key(file, uarg);
3300 #ifdef CONFIG_LL_ENCRYPTION
3301                 if (!rc && S_ISDIR(inode->i_mode))
3302                         sptlrpc_enc_pool_add_user();
3303 #endif
3304                 RETURN(rc);
3305         case LL_IOC_GET_ENCRYPTION_KEY_STATUS:
3306                 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
3307                         return -EOPNOTSUPP;
3308                 RETURN(llcrypt_ioctl_get_key_status(file, uarg));
3309         case LL_IOC_GET_ENCRYPTION_POLICY_EX:
3310                 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
3311                         return -EOPNOTSUPP;
3312                 RETURN(llcrypt_ioctl_get_policy_ex(file, uarg));
3313         case LL_IOC_SET_ENCRYPTION_POLICY:
3314                 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
3315                         return -EOPNOTSUPP;
3316                 RETURN(llcrypt_ioctl_set_policy(file, uarg));
3317         case LL_IOC_REMOVE_ENCRYPTION_KEY:
3318                 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
3319                         return -EOPNOTSUPP;
3320                 RETURN(llcrypt_ioctl_remove_key(file, uarg));
3321         case LL_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
3322                 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
3323                         return -EOPNOTSUPP;
3324                 RETURN(llcrypt_ioctl_remove_key_all_users(file, uarg));
3325 #endif
3326         case LL_IOC_GETPARENT:
3327                 RETURN(ll_getparent(file, uarg));
3328         case LL_IOC_PATH2FID:
3329                 if (copy_to_user(uarg, ll_inode2fid(inode),
3330                                  sizeof(struct lu_fid)))
3331                         RETURN(-EFAULT);
3332                 RETURN(0);
3333         case LL_IOC_UNLOCK_FOREIGN: {
3334                 struct dentry *dentry = file_dentry(file);
3335
3336                 /* if not a foreign symlink do nothing */
3337                 if (ll_foreign_is_removable(dentry, true)) {
3338                         CDEBUG(D_INFO,
3339                                "prevent unlink of non-foreign file ("DFID")\n",
3340                                PFID(ll_inode2fid(inode)));
3341                         RETURN(-EOPNOTSUPP);
3342                 }
3343                 RETURN(0);
3344         }
3345         case OBD_IOC_FID2PATH:
3346                 RETURN(ll_fid2path(inode, uarg));
3347 #ifdef OBD_IOC_GETNAME_OLD
3348         case_OBD_IOC_DEPRECATED_FT(OBD_IOC_GETNAME_OLD,
3349                                    ll_i2sbi(inode)->ll_md_exp->exp_obd->obd_name,
3350                                    2, 16);
3351 #endif
3352         case OBD_IOC_GETDTNAME:
3353         case OBD_IOC_GETMDNAME:
3354                 RETURN(ll_get_obd_name(inode, cmd, uarg));
3355         default:
3356                 RETURN(-ENOTTY);
3357         }
3358
3359         RETURN(0);
3360 }
3361
3362 int ll_flush_ctx(struct inode *inode)
3363 {
3364         struct ll_sb_info  *sbi = ll_i2sbi(inode);
3365
3366         CDEBUG(D_SEC, "flush context for user %d\n",
3367                from_kuid(&init_user_ns, current_uid()));
3368
3369         obd_set_info_async(NULL, sbi->ll_md_exp,
3370                            sizeof(KEY_FLUSH_CTX), KEY_FLUSH_CTX,
3371                            0, NULL, NULL);
3372         obd_set_info_async(NULL, sbi->ll_dt_exp,
3373                            sizeof(KEY_FLUSH_CTX), KEY_FLUSH_CTX,
3374                            0, NULL, NULL);
3375         return 0;
3376 }
3377
3378 /* umount -f client means force down, don't save state */
3379 void ll_umount_begin(struct super_block *sb)
3380 {
3381         struct ll_sb_info *sbi = ll_s2sbi(sb);
3382         struct obd_device *obd;
3383         struct obd_ioctl_data *ioc_data;
3384         int cnt;
3385         ENTRY;
3386
3387         CDEBUG(D_VFSTRACE, "VFS Op: superblock %p count %d active %d\n", sb,
3388                sb->s_count, atomic_read(&sb->s_active));
3389
3390         obd = class_exp2obd(sbi->ll_md_exp);
3391         if (obd == NULL) {
3392                 CERROR("Invalid MDC connection handle %#llx\n",
3393                        sbi->ll_md_exp->exp_handle.h_cookie);
3394                 EXIT;
3395                 return;
3396         }
3397         obd->obd_force = 1;
3398
3399         obd = class_exp2obd(sbi->ll_dt_exp);
3400         if (obd == NULL) {
3401                 CERROR("Invalid LOV connection handle %#llx\n",
3402                        sbi->ll_dt_exp->exp_handle.h_cookie);
3403                 EXIT;
3404                 return;
3405         }
3406         obd->obd_force = 1;
3407
3408         OBD_ALLOC_PTR(ioc_data);
3409         if (ioc_data) {
3410                 obd_iocontrol(OBD_IOC_SET_ACTIVE, sbi->ll_md_exp,
3411                               sizeof *ioc_data, ioc_data, NULL);
3412
3413                 obd_iocontrol(OBD_IOC_SET_ACTIVE, sbi->ll_dt_exp,
3414                               sizeof *ioc_data, ioc_data, NULL);
3415
3416                 OBD_FREE_PTR(ioc_data);
3417         }
3418
3419         /* Really, we'd like to wait until there are no requests outstanding,
3420          * and then continue.  For now, we just periodically checking for vfs
3421          * to decrement mnt_cnt and hope to finish it within 10sec.
3422          */
3423         cnt = 10;
3424         while (cnt > 0 &&
3425                !may_umount(sbi->ll_mnt.mnt)) {
3426                 ssleep(1);
3427                 cnt -= 1;
3428         }
3429
3430         EXIT;
3431 }
3432
3433 int ll_remount_fs(struct super_block *sb, int *flags, char *data)
3434 {
3435         struct ll_sb_info *sbi = ll_s2sbi(sb);
3436         char *profilenm = get_profile_name(sb);
3437         int err;
3438         __u32 read_only;
3439
3440         if ((*flags & MS_RDONLY) != (sb->s_flags & SB_RDONLY)) {
3441                 read_only = *flags & MS_RDONLY;
3442                 err = obd_set_info_async(NULL, sbi->ll_md_exp,
3443                                          sizeof(KEY_READ_ONLY),
3444                                          KEY_READ_ONLY, sizeof(read_only),
3445                                          &read_only, NULL);
3446                 if (err) {
3447                         LCONSOLE_WARN("Failed to remount %s %s (%d)\n",
3448                                       profilenm, read_only ?
3449                                       "read-only" : "read-write", err);
3450                         return err;
3451                 }
3452
3453                 if (read_only)
3454                         sb->s_flags |= SB_RDONLY;
3455                 else
3456                         sb->s_flags &= ~SB_RDONLY;
3457
3458                 if (test_bit(LL_SBI_VERBOSE, sbi->ll_flags))
3459                         LCONSOLE_WARN("Remounted %s %s\n", profilenm,
3460                                       read_only ?  "read-only" : "read-write");
3461         }
3462         return 0;
3463 }
3464
3465 /**
3466  * Cleanup the open handle that is cached on MDT-side.
3467  *
3468  * For open case, the client side open handling thread may hit error
3469  * after the MDT grant the open. Under such case, the client should
3470  * send close RPC to the MDT as cleanup; otherwise, the open handle
3471  * on the MDT will be leaked there until the client umount or evicted.
3472  *
3473  * In further, if someone unlinked the file, because the open handle
3474  * holds the reference on such file/object, then it will block the
3475  * subsequent threads that want to locate such object via FID.
3476  *
3477  * \param[in] sb        super block for this file-system
3478  * \param[in] open_req  pointer to the original open request
3479  */
3480 void ll_open_cleanup(struct super_block *sb, struct req_capsule *pill)
3481 {
3482         struct mdt_body                 *body;
3483         struct md_op_data               *op_data;
3484         struct ptlrpc_request           *close_req = NULL;
3485         struct obd_export               *exp       = ll_s2sbi(sb)->ll_md_exp;
3486         ENTRY;
3487
3488         body = req_capsule_server_get(pill, &RMF_MDT_BODY);
3489         OBD_ALLOC_PTR(op_data);
3490         if (op_data == NULL) {
3491                 CWARN("%s: cannot allocate op_data to release open handle for "
3492                       DFID"\n", ll_s2sbi(sb)->ll_fsname, PFID(&body->mbo_fid1));
3493
3494                 RETURN_EXIT;
3495         }
3496
3497         op_data->op_fid1 = body->mbo_fid1;
3498         op_data->op_open_handle = body->mbo_open_handle;
3499         op_data->op_mod_time = ktime_get_real_seconds();
3500         md_close(exp, op_data, NULL, &close_req);
3501         ptlrpc_req_finished(close_req);
3502         ll_finish_md_op_data(op_data);
3503
3504         EXIT;
3505 }
3506
3507 /* set filesystem-wide default LMV for subdir mount if it's enabled on ROOT. */
3508 static int ll_fileset_default_lmv_fixup(struct inode *inode,
3509                                         struct lustre_md *md)
3510 {
3511         struct ll_sb_info *sbi = ll_i2sbi(inode);
3512         struct ptlrpc_request *req = NULL;
3513         union lmv_mds_md *lmm = NULL;
3514         int size = 0;
3515         int rc;
3516
3517         LASSERT(is_root_inode(inode));
3518         LASSERT(!fid_is_root(&sbi->ll_root_fid));
3519         LASSERT(!md->default_lmv);
3520
3521         rc = ll_dir_get_default_layout(inode, (void **)&lmm, &size, &req,
3522                                        OBD_MD_DEFAULT_MEA,
3523                                        GET_DEFAULT_LAYOUT_ROOT);
3524         if (rc && rc != -ENODATA)
3525                 GOTO(out, rc);
3526
3527         rc = 0;
3528         if (lmm && size) {
3529                 rc = md_unpackmd(sbi->ll_md_exp, &md->default_lmv, lmm, size);
3530                 if (rc < 0)
3531                         GOTO(out, rc);
3532
3533                 rc = 0;
3534         }
3535         EXIT;
3536 out:
3537         if (req)
3538                 ptlrpc_req_finished(req);
3539         return rc;
3540 }
3541
3542 int ll_prep_inode(struct inode **inode, struct req_capsule *pill,
3543                   struct super_block *sb, struct lookup_intent *it)
3544 {
3545         struct ll_sb_info *sbi = NULL;
3546         struct lustre_md md = { NULL };
3547         bool default_lmv_deleted = false;
3548         int rc;
3549
3550         ENTRY;
3551
3552         LASSERT(*inode || sb);
3553         sbi = sb ? ll_s2sbi(sb) : ll_i2sbi(*inode);
3554         rc = md_get_lustre_md(sbi->ll_md_exp, pill, sbi->ll_dt_exp,
3555                               sbi->ll_md_exp, &md);
3556         if (rc != 0)
3557                 GOTO(out, rc);
3558
3559         /*
3560          * clear default_lmv only if intent_getattr reply doesn't contain it.
3561          * but it needs to be done after iget, check this early because
3562          * ll_update_lsm_md() may change md.
3563          */
3564         if (it && (it->it_op & (IT_LOOKUP | IT_GETATTR)) &&
3565             S_ISDIR(md.body->mbo_mode) && !md.default_lmv) {
3566                 if (unlikely(*inode && is_root_inode(*inode) &&
3567                              !fid_is_root(&sbi->ll_root_fid))) {
3568                         rc = ll_fileset_default_lmv_fixup(*inode, &md);
3569                         if (rc)
3570                                 GOTO(out, rc);
3571                 }
3572
3573                 if (!md.default_lmv)
3574                         default_lmv_deleted = true;
3575         }
3576
3577         if (*inode) {
3578                 rc = ll_update_inode(*inode, &md);
3579                 if (rc != 0)
3580                         GOTO(out, rc);
3581         } else {
3582                 bool api32 = test_bit(LL_SBI_32BIT_API, sbi->ll_flags);
3583                 struct lu_fid *fid1 = &md.body->mbo_fid1;
3584
3585                 LASSERT(sb != NULL);
3586
3587                 /*
3588                  * At this point server returns to client's same fid as client
3589                  * generated for creating. So using ->fid1 is okay here.
3590                  */
3591                 if (!fid_is_sane(fid1)) {
3592                         CERROR("%s: Fid is insane "DFID"\n",
3593                                 sbi->ll_fsname, PFID(fid1));
3594                         GOTO(out, rc = -EINVAL);
3595                 }
3596
3597                 *inode = ll_iget(sb, cl_fid_build_ino(fid1, api32), &md);
3598                 if (IS_ERR(*inode)) {
3599                         lmd_clear_acl(&md);
3600                         rc = IS_ERR(*inode) ? PTR_ERR(*inode) : -ENOMEM;
3601                         *inode = NULL;
3602                         CERROR("new_inode -fatal: rc %d\n", rc);
3603                         GOTO(out, rc);
3604                 }
3605         }
3606
3607         /* Handling piggyback layout lock.
3608          * Layout lock can be piggybacked by getattr and open request.
3609          * The lsm can be applied to inode only if it comes with a layout lock
3610          * otherwise correct layout may be overwritten, for example:
3611          * 1. proc1: mdt returns a lsm but not granting layout
3612          * 2. layout was changed by another client
3613          * 3. proc2: refresh layout and layout lock granted
3614          * 4. proc1: to apply a stale layout */
3615         if (it != NULL && it->it_lock_mode != 0) {
3616                 struct lustre_handle lockh;
3617                 struct ldlm_lock *lock;
3618
3619                 lockh.cookie = it->it_lock_handle;
3620                 lock = ldlm_handle2lock(&lockh);
3621                 LASSERT(lock != NULL);
3622                 if (ldlm_has_layout(lock)) {
3623                         struct cl_object_conf conf;
3624
3625                         memset(&conf, 0, sizeof(conf));
3626                         conf.coc_opc = OBJECT_CONF_SET;
3627                         conf.coc_inode = *inode;
3628                         conf.coc_lock = lock;
3629                         conf.u.coc_layout = md.layout;
3630                         (void)ll_layout_conf(*inode, &conf);
3631                 }
3632                 LDLM_LOCK_PUT(lock);
3633         }
3634
3635         if (default_lmv_deleted)
3636                 ll_update_default_lsm_md(*inode, &md);
3637
3638         /* we may want to apply some policy for foreign file/dir */
3639         if (ll_sbi_has_foreign_symlink(sbi)) {
3640                 rc = ll_manage_foreign(*inode, &md);
3641                 if (rc < 0)
3642                         GOTO(out, rc);
3643         }
3644
3645         GOTO(out, rc = 0);
3646
3647 out:
3648         /* cleanup will be done if necessary */
3649         md_free_lustre_md(sbi->ll_md_exp, &md);
3650
3651         if (rc != 0 && it != NULL && it->it_op & IT_OPEN) {
3652                 ll_intent_drop_lock(it);
3653                 ll_open_cleanup(sb != NULL ? sb : (*inode)->i_sb, pill);
3654         }
3655
3656         return rc;
3657 }
3658
3659 int ll_obd_statfs(struct inode *inode, void __user *uarg)
3660 {
3661         struct ll_sb_info *sbi = NULL;
3662         struct obd_export *exp;
3663         struct obd_ioctl_data *data = NULL;
3664         __u32 type;
3665         int len = 0, rc;
3666
3667         if (inode)
3668                 sbi = ll_i2sbi(inode);
3669         if (!sbi)
3670                 GOTO(out_statfs, rc = -EINVAL);
3671
3672         rc = obd_ioctl_getdata(&data, &len, uarg);
3673         if (rc)
3674                 GOTO(out_statfs, rc);
3675
3676         if (!data->ioc_inlbuf1 || !data->ioc_inlbuf2 ||
3677             !data->ioc_pbuf1 || !data->ioc_pbuf2)
3678                 GOTO(out_statfs, rc = -EINVAL);
3679
3680         if (data->ioc_inllen1 != sizeof(__u32) ||
3681             data->ioc_inllen2 != sizeof(__u32) ||
3682             data->ioc_plen1 != sizeof(struct obd_statfs) ||
3683             data->ioc_plen2 != sizeof(struct obd_uuid))
3684                 GOTO(out_statfs, rc = -EINVAL);
3685
3686         memcpy(&type, data->ioc_inlbuf1, sizeof(__u32));
3687         if (type & LL_STATFS_LMV)
3688                 exp = sbi->ll_md_exp;
3689         else if (type & LL_STATFS_LOV)
3690                 exp = sbi->ll_dt_exp;
3691         else
3692                 GOTO(out_statfs, rc = -ENODEV);
3693
3694         rc = obd_iocontrol(IOC_OBD_STATFS, exp, len, data, NULL);
3695         if (rc)
3696                 GOTO(out_statfs, rc);
3697 out_statfs:
3698         OBD_FREE_LARGE(data, len);
3699         return rc;
3700 }
3701
3702 /*
3703  * this is normally called in ll_fini_md_op_data(), but sometimes it needs to
3704  * be called early to avoid deadlock.
3705  */
3706 void ll_unlock_md_op_lsm(struct md_op_data *op_data)
3707 {
3708         if (op_data->op_mea2_sem) {
3709                 up_read_non_owner(op_data->op_mea2_sem);
3710                 op_data->op_mea2_sem = NULL;
3711         }
3712
3713         if (op_data->op_mea1_sem) {
3714                 up_read_non_owner(op_data->op_mea1_sem);
3715                 op_data->op_mea1_sem = NULL;
3716         }
3717 }
3718
3719 /* this function prepares md_op_data hint for passing it down to MD stack. */
3720 struct md_op_data *ll_prep_md_op_data(struct md_op_data *op_data,
3721                                       struct inode *i1, struct inode *i2,
3722                                       const char *name, size_t namelen,
3723                                       __u32 mode, enum md_op_code opc,
3724                                       void *data)
3725 {
3726         struct llcrypt_name fname = { 0 };
3727         int rc;
3728
3729         LASSERT(i1 != NULL);
3730
3731         if (name == NULL) {
3732                 /* Do not reuse namelen for something else. */
3733                 if (namelen != 0)
3734                         return ERR_PTR(-EINVAL);
3735         } else {
3736                 if ((!IS_ENCRYPTED(i1) ||
3737                      (opc != LUSTRE_OPC_LOOKUP && opc != LUSTRE_OPC_CREATE)) &&
3738                     namelen > ll_i2sbi(i1)->ll_namelen)
3739                         return ERR_PTR(-ENAMETOOLONG);
3740
3741                 /* "/" is not valid name, but it's allowed */
3742                 if (!lu_name_is_valid_2(name, namelen) &&
3743                     strncmp("/", name, namelen) != 0)
3744                         return ERR_PTR(-EINVAL);
3745         }
3746
3747         if (op_data == NULL)
3748                 OBD_ALLOC_PTR(op_data);
3749
3750         if (op_data == NULL)
3751                 return ERR_PTR(-ENOMEM);
3752
3753         ll_i2gids(op_data->op_suppgids, i1, i2);
3754         /* If the client is using a subdir mount and looks at what it sees as
3755          * /.fscrypt, interpret it as the .fscrypt dir at the root of the fs.
3756          */
3757         if (unlikely(i1->i_sb && i1->i_sb->s_root && is_root_inode(i1) &&
3758                      !fid_is_root(ll_inode2fid(i1)) &&
3759                      name && namelen == strlen(dot_fscrypt_name) &&
3760                      strncmp(name, dot_fscrypt_name, namelen) == 0))
3761                 lu_root_fid(&op_data->op_fid1);
3762         else
3763                 op_data->op_fid1 = *ll_inode2fid(i1);
3764
3765         if (S_ISDIR(i1->i_mode)) {
3766                 down_read_non_owner(&ll_i2info(i1)->lli_lsm_sem);
3767                 op_data->op_mea1_sem = &ll_i2info(i1)->lli_lsm_sem;
3768                 op_data->op_mea1 = ll_i2info(i1)->lli_lsm_md;
3769                 op_data->op_default_mea1 = ll_i2info(i1)->lli_default_lsm_md;
3770         }
3771
3772         if (i2) {
3773                 op_data->op_fid2 = *ll_inode2fid(i2);
3774                 if (S_ISDIR(i2->i_mode)) {
3775                         if (i2 != i1) {
3776                                 /* i2 is typically a child of i1, and MUST be
3777                                  * further from the root to avoid deadlocks.
3778                                  */
3779                                 down_read_non_owner(&ll_i2info(i2)->lli_lsm_sem);
3780                                 op_data->op_mea2_sem =
3781                                                 &ll_i2info(i2)->lli_lsm_sem;
3782                         }
3783                         op_data->op_mea2 = ll_i2info(i2)->lli_lsm_md;
3784                 }
3785         } else {
3786                 fid_zero(&op_data->op_fid2);
3787         }
3788
3789         if (test_bit(LL_SBI_64BIT_HASH, ll_i2sbi(i1)->ll_flags))
3790                 op_data->op_cli_flags |= CLI_HASH64;
3791
3792         if (ll_need_32bit_api(ll_i2sbi(i1)))
3793                 op_data->op_cli_flags |= CLI_API32;
3794
3795         if ((i2 && is_root_inode(i2)) ||
3796             opc == LUSTRE_OPC_LOOKUP || opc == LUSTRE_OPC_CREATE) {
3797                 /* In case of lookup, ll_setup_filename() has already been
3798                  * called in ll_lookup_it(), so just take provided name.
3799                  * Also take provided name if we are dealing with root inode.
3800                  */
3801                 fname.disk_name.name = (unsigned char *)name;
3802                 fname.disk_name.len = namelen;
3803         } else if (name && namelen) {
3804                 struct qstr dname = QSTR_INIT(name, namelen);
3805                 struct inode *dir;
3806                 struct lu_fid *pfid = NULL;
3807                 struct lu_fid fid;
3808                 int lookup;
3809
3810                 if (!S_ISDIR(i1->i_mode) && i2 && S_ISDIR(i2->i_mode)) {
3811                         /* special case when called from ll_link() */
3812                         dir = i2;
3813                         lookup = 0;
3814                 } else {
3815                         dir = i1;
3816                         lookup = (int)(opc == LUSTRE_OPC_ANY);
3817                 }
3818                 if (opc == LUSTRE_OPC_ANY && lookup)
3819                         pfid = &fid;
3820                 rc = ll_setup_filename(dir, &dname, lookup, &fname, pfid);
3821                 if (rc) {
3822                         ll_finish_md_op_data(op_data);
3823                         return ERR_PTR(rc);
3824                 }
3825                 if (pfid && !fid_is_zero(pfid)) {
3826                         if (i2 == NULL)
3827                                 op_data->op_fid2 = fid;
3828                         op_data->op_bias = MDS_FID_OP;
3829                 }
3830                 if (fname.disk_name.name &&
3831                     fname.disk_name.name != (unsigned char *)name) {
3832                         /* op_data->op_name must be freed after use */
3833                         op_data->op_flags |= MF_OPNAME_KMALLOCED;
3834                 }
3835         }
3836
3837         /* In fact LUSTRE_OPC_LOOKUP, LUSTRE_OPC_OPEN
3838          * are LUSTRE_OPC_ANY
3839          */
3840         if (opc == LUSTRE_OPC_LOOKUP || opc == LUSTRE_OPC_OPEN)
3841                 op_data->op_code = LUSTRE_OPC_ANY;
3842         else
3843                 op_data->op_code = opc;
3844         op_data->op_name = fname.disk_name.name;
3845         op_data->op_namelen = fname.disk_name.len;
3846         op_data->op_mode = mode;
3847         op_data->op_mod_time = ktime_get_real_seconds();
3848         op_data->op_fsuid = from_kuid(&init_user_ns, current_fsuid());
3849         op_data->op_fsgid = from_kgid(&init_user_ns, current_fsgid());
3850         op_data->op_cap = current_cap();
3851         op_data->op_mds = 0;
3852         if ((opc == LUSTRE_OPC_CREATE) && (name != NULL) &&
3853              filename_is_volatile(name, namelen, &op_data->op_mds)) {
3854                 op_data->op_bias |= MDS_CREATE_VOLATILE;
3855         }
3856         op_data->op_data = data;
3857
3858         return op_data;
3859 }
3860
3861 void ll_finish_md_op_data(struct md_op_data *op_data)
3862 {
3863         ll_unlock_md_op_lsm(op_data);
3864         ll_security_release_secctx(op_data->op_file_secctx,
3865                                    op_data->op_file_secctx_size,
3866                                    op_data->op_file_secctx_slot);
3867         if (op_data->op_flags & MF_OPNAME_KMALLOCED)
3868                 /* allocated via ll_setup_filename called
3869                  * from ll_prep_md_op_data
3870                  */
3871                 kfree(op_data->op_name);
3872         llcrypt_free_ctx(op_data->op_file_encctx, op_data->op_file_encctx_size);
3873         OBD_FREE_PTR(op_data);
3874 }
3875
3876 int ll_show_options(struct seq_file *seq, struct dentry *dentry)
3877 {
3878         struct ll_sb_info *sbi;
3879         int i;
3880
3881         LASSERT(seq && dentry);
3882         sbi = ll_s2sbi(dentry->d_sb);
3883
3884         if (test_bit(LL_SBI_NOLCK, sbi->ll_flags))
3885                 seq_puts(seq, "nolock");
3886
3887         for (i = 1; ll_sbi_flags_name[i].token != LL_SBI_NUM_MOUNT_OPT; i++) {
3888                 /* match_table in some cases has patterns for both enabled and
3889                  * disabled cases. Ignore 'no'xxx versions if bit is set.
3890                  */
3891                 if (test_bit(ll_sbi_flags_name[i].token, sbi->ll_flags) &&
3892                     strncmp(ll_sbi_flags_name[i].pattern, "no", 2)) {
3893                         if (ll_sbi_flags_name[i].token ==
3894                             LL_SBI_FOREIGN_SYMLINK) {
3895                                 seq_show_option(seq, "foreign_symlink",
3896                                                 sbi->ll_foreign_symlink_prefix);
3897                         } else {
3898                                 seq_printf(seq, ",%s",
3899                                            ll_sbi_flags_name[i].pattern);
3900                         }
3901
3902                         /* You can have either localflock or flock but not
3903                          * both. If localflock is set don't print flock or
3904                          * noflock.
3905                          */
3906                         if (ll_sbi_flags_name[i].token == LL_SBI_LOCALFLOCK)
3907                                 i += 2;
3908                 } else if (!test_bit(ll_sbi_flags_name[i].token, sbi->ll_flags) &&
3909                            !strncmp(ll_sbi_flags_name[i].pattern, "no", 2)) {
3910                         seq_printf(seq, ",%s",
3911                                    ll_sbi_flags_name[i].pattern);
3912                 }
3913         }
3914
3915         llcrypt_show_test_dummy_encryption(seq, ',', dentry->d_sb);
3916
3917         RETURN(0);
3918 }
3919
3920 /**
3921  * Get obd name by cmd, and copy out to user space
3922  */
3923 int ll_get_obd_name(struct inode *inode, unsigned int cmd, void __user *uarg)
3924 {
3925         struct ll_sb_info *sbi = ll_i2sbi(inode);
3926         struct obd_device *obd;
3927         ENTRY;
3928
3929         if (cmd == OBD_IOC_GETNAME_OLD || cmd == OBD_IOC_GETDTNAME)
3930                 obd = class_exp2obd(sbi->ll_dt_exp);
3931         else if (cmd == OBD_IOC_GETMDNAME)
3932                 obd = class_exp2obd(sbi->ll_md_exp);
3933         else
3934                 RETURN(-EINVAL);
3935
3936         if (!obd)
3937                 RETURN(-ENOENT);
3938
3939         if (copy_to_user(uarg, obd->obd_name, strlen(obd->obd_name) + 1))
3940                 RETURN(-EFAULT);
3941
3942         RETURN(0);
3943 }
3944
3945 struct dname_buf {
3946         struct work_struct db_work;
3947         struct dentry *db_dentry;
3948         /* Let's hope the path is not too long, 32 bytes for the work struct
3949          * on my kernel
3950          */
3951         char buf[PAGE_SIZE - sizeof(struct work_struct) - sizeof(void *)];
3952 };
3953
3954 static void ll_dput_later(struct work_struct *work)
3955 {
3956         struct dname_buf *db = container_of(work, struct dname_buf, db_work);
3957
3958         dput(db->db_dentry);
3959         free_page((unsigned long)db);
3960 }
3961
3962 static char* ll_d_path(struct dentry *dentry, char *buf, int bufsize)
3963 {
3964         char *path = NULL;
3965
3966         struct path p;
3967
3968         p.dentry = dentry;
3969         p.mnt = current->fs->root.mnt;
3970         path_get(&p);
3971         path = d_path(&p, buf, bufsize);
3972         path_put(&p);
3973         return path;
3974 }
3975
3976 void ll_dirty_page_discard_warn(struct inode *inode, int ioret)
3977 {
3978         struct dname_buf *db;
3979         char  *path = NULL;
3980         struct dentry *dentry = NULL;
3981
3982         /* this can be called inside spin lock so use GFP_ATOMIC. */
3983         db = (struct dname_buf *)__get_free_page(GFP_ATOMIC);
3984         if (db != NULL) {
3985
3986                 dentry = d_find_alias(inode);
3987                 if (dentry != NULL)
3988                         path = ll_d_path(dentry, db->buf, sizeof(db->buf));
3989         }
3990
3991         /* The below message is checked in recovery-small.sh test_24b */
3992         CDEBUG(D_WARNING,
3993                "%s: dirty page discard: %s/fid: "DFID"/%s may get corrupted "
3994                "(rc %d)\n", ll_i2sbi(inode)->ll_fsname,
3995                s2lsi(inode->i_sb)->lsi_lmd->lmd_dev,
3996                PFID(ll_inode2fid(inode)),
3997                (path && !IS_ERR(path)) ? path : "", ioret);
3998
3999         if (dentry != NULL) {
4000                 /* We cannot dput here since if we happen to be the last holder
4001                  * then we can end up waiting for page evictions that
4002                  * in turn wait for RPCs that need this instance of ptlrpcd
4003                  * (callng brw_interpret->*page_completion*->vmpage_error->here)
4004                  * LU-15340
4005                  */
4006                 INIT_WORK(&db->db_work, ll_dput_later);
4007                 db->db_dentry = dentry;
4008                 schedule_work(&db->db_work);
4009         } else {
4010                 if (db != NULL)
4011                         free_page((unsigned long)db);
4012         }
4013 }
4014
4015 ssize_t ll_copy_user_md(const struct lov_user_md __user *md,
4016                         struct lov_user_md **kbuf)
4017 {
4018         struct lov_user_md      lum;
4019         ssize_t                 lum_size;
4020         ENTRY;
4021
4022         if (copy_from_user(&lum, md, sizeof(lum)))
4023                 RETURN(-EFAULT);
4024
4025         lum_size = ll_lov_user_md_size(&lum);
4026         if (lum_size < 0)
4027                 RETURN(lum_size);
4028
4029         OBD_ALLOC_LARGE(*kbuf, lum_size);
4030         if (*kbuf == NULL)
4031                 RETURN(-ENOMEM);
4032
4033         if (copy_from_user(*kbuf, md, lum_size) != 0) {
4034                 OBD_FREE_LARGE(*kbuf, lum_size);
4035                 RETURN(-EFAULT);
4036         }
4037
4038         RETURN(lum_size);
4039 }
4040
4041 /*
4042  * Compute llite root squash state after a change of root squash
4043  * configuration setting or add/remove of a lnet nid
4044  */
4045 void ll_compute_rootsquash_state(struct ll_sb_info *sbi)
4046 {
4047         struct root_squash_info *squash = &sbi->ll_squash;
4048         int i;
4049         bool matched;
4050         struct lnet_processid id;
4051
4052         /* Update norootsquash flag */
4053         spin_lock(&squash->rsi_lock);
4054         if (list_empty(&squash->rsi_nosquash_nids))
4055                 clear_bit(LL_SBI_NOROOTSQUASH, sbi->ll_flags);
4056         else {
4057                 /* Do not apply root squash as soon as one of our NIDs is
4058                  * in the nosquash_nids list */
4059                 matched = false;
4060                 i = 0;
4061                 while (LNetGetId(i++, &id) != -ENOENT) {
4062                         if (nid_is_lo0(&id.nid))
4063                                 continue;
4064                         if (cfs_match_nid(&id.nid,
4065                                           &squash->rsi_nosquash_nids)) {
4066                                 matched = true;
4067                                 break;
4068                         }
4069                 }
4070                 if (matched)
4071                         set_bit(LL_SBI_NOROOTSQUASH, sbi->ll_flags);
4072                 else
4073                         clear_bit(LL_SBI_NOROOTSQUASH, sbi->ll_flags);
4074         }
4075         spin_unlock(&squash->rsi_lock);
4076 }
4077
4078 /**
4079  * Parse linkea content to extract information about a given hardlink
4080  *
4081  * \param[in]   ldata      - Initialized linkea data
4082  * \param[in]   linkno     - Link identifier
4083  * \param[out]  parent_fid - The entry's parent FID
4084  * \param[out]  ln         - Entry name destination buffer
4085  *
4086  * \retval 0 on success
4087  * \retval Appropriate negative error code on failure
4088  */
4089 static int ll_linkea_decode(struct linkea_data *ldata, unsigned int linkno,
4090                             struct lu_fid *parent_fid, struct lu_name *ln)
4091 {
4092         unsigned int    idx;
4093         int             rc;
4094         ENTRY;
4095
4096         rc = linkea_init_with_rec(ldata);
4097         if (rc < 0)
4098                 RETURN(rc);
4099
4100         if (linkno >= ldata->ld_leh->leh_reccount)
4101                 /* beyond last link */
4102                 RETURN(-ENODATA);
4103
4104         linkea_first_entry(ldata);
4105         for (idx = 0; ldata->ld_lee != NULL; idx++) {
4106                 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen, ln,
4107                                     parent_fid);
4108                 if (idx == linkno)
4109                         break;
4110
4111                 linkea_next_entry(ldata);
4112         }
4113
4114         if (idx < linkno)
4115                 RETURN(-ENODATA);
4116
4117         RETURN(0);
4118 }
4119
4120 /**
4121  * Get parent FID and name of an identified link. Operation is performed for
4122  * a given link number, letting the caller iterate over linkno to list one or
4123  * all links of an entry.
4124  *
4125  * \param[in]     file - File descriptor against which to perform the operation
4126  * \param[in,out] arg  - User-filled structure containing the linkno to operate
4127  *                       on and the available size. It is eventually filled with
4128  *                       the requested information or left untouched on error
4129  *
4130  * \retval - 0 on success
4131  * \retval - Appropriate negative error code on failure
4132  */
4133 int ll_getparent(struct file *file, struct getparent __user *arg)
4134 {
4135         struct inode            *inode = file_inode(file);
4136         struct linkea_data      *ldata;
4137         struct lu_buf            buf = LU_BUF_NULL;
4138         struct lu_name           ln;
4139         struct lu_fid            parent_fid;
4140         __u32                    linkno;
4141         __u32                    name_size;
4142         int                      rc;
4143
4144         ENTRY;
4145
4146         if (!capable(CAP_DAC_READ_SEARCH) &&
4147             !test_bit(LL_SBI_USER_FID2PATH, ll_i2sbi(inode)->ll_flags))
4148                 RETURN(-EPERM);
4149
4150         if (get_user(name_size, &arg->gp_name_size))
4151                 RETURN(-EFAULT);
4152
4153         if (get_user(linkno, &arg->gp_linkno))
4154                 RETURN(-EFAULT);
4155
4156         if (name_size > PATH_MAX)
4157                 RETURN(-EINVAL);
4158
4159         OBD_ALLOC(ldata, sizeof(*ldata));
4160         if (ldata == NULL)
4161                 RETURN(-ENOMEM);
4162
4163         rc = linkea_data_new(ldata, &buf);
4164         if (rc < 0)
4165                 GOTO(ldata_free, rc);
4166
4167         rc = ll_xattr_list(inode, XATTR_NAME_LINK, XATTR_TRUSTED_T, buf.lb_buf,
4168                            buf.lb_len, OBD_MD_FLXATTR);
4169         if (rc < 0)
4170                 GOTO(lb_free, rc);
4171
4172         rc = ll_linkea_decode(ldata, linkno, &parent_fid, &ln);
4173         if (rc < 0)
4174                 GOTO(lb_free, rc);
4175
4176         if (ln.ln_namelen >= name_size)
4177                 GOTO(lb_free, rc = -EOVERFLOW);
4178
4179         if (copy_to_user(&arg->gp_fid, &parent_fid, sizeof(arg->gp_fid)))
4180                 GOTO(lb_free, rc = -EFAULT);
4181
4182         if (copy_to_user(&arg->gp_name, ln.ln_name, ln.ln_namelen))
4183                 GOTO(lb_free, rc = -EFAULT);
4184
4185         if (put_user('\0', arg->gp_name + ln.ln_namelen))
4186                 GOTO(lb_free, rc = -EFAULT);
4187
4188 lb_free:
4189         lu_buf_free(&buf);
4190 ldata_free:
4191         OBD_FREE(ldata, sizeof(*ldata));
4192
4193         RETURN(rc);
4194 }