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