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