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