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