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