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