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