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