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