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