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