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