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