GNU Linux-libre 4.14.290-gnu1
[releases.git] / drivers / staging / lustre / 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, 2015, 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/module.h>
40 #include <linux/statfs.h>
41 #include <linux/types.h>
42 #include <linux/mm.h>
43
44 #include <uapi/linux/lustre/lustre_ioctl.h>
45 #include <lustre_ha.h>
46 #include <lustre_dlm.h>
47 #include <lprocfs_status.h>
48 #include <lustre_disk.h>
49 #include <uapi/linux/lustre/lustre_param.h>
50 #include <lustre_log.h>
51 #include <cl_object.h>
52 #include <obd_cksum.h>
53 #include "llite_internal.h"
54
55 struct kmem_cache *ll_file_data_slab;
56 struct dentry *llite_root;
57 struct kset *llite_kset;
58
59 #ifndef log2
60 #define log2(n) ffz(~(n))
61 #endif
62
63 static struct ll_sb_info *ll_init_sbi(struct super_block *sb)
64 {
65         struct ll_sb_info *sbi = NULL;
66         unsigned long pages;
67         unsigned long lru_page_max;
68         struct sysinfo si;
69         class_uuid_t uuid;
70         int i;
71
72         sbi = kzalloc(sizeof(*sbi), GFP_NOFS);
73         if (!sbi)
74                 return NULL;
75
76         spin_lock_init(&sbi->ll_lock);
77         mutex_init(&sbi->ll_lco.lco_lock);
78         spin_lock_init(&sbi->ll_pp_extent_lock);
79         spin_lock_init(&sbi->ll_process_lock);
80         sbi->ll_rw_stats_on = 0;
81
82         si_meminfo(&si);
83         pages = si.totalram - si.totalhigh;
84         lru_page_max = pages / 2;
85
86         sbi->ll_cache = cl_cache_init(lru_page_max);
87         if (!sbi->ll_cache) {
88                 kfree(sbi);
89                 return NULL;
90         }
91
92         sbi->ll_ra_info.ra_max_pages_per_file = min(pages / 32,
93                                            SBI_DEFAULT_READAHEAD_MAX);
94         sbi->ll_ra_info.ra_max_pages = sbi->ll_ra_info.ra_max_pages_per_file;
95         sbi->ll_ra_info.ra_max_read_ahead_whole_pages =
96                                            SBI_DEFAULT_READAHEAD_WHOLE_MAX;
97
98         ll_generate_random_uuid(uuid);
99         class_uuid_unparse(uuid, &sbi->ll_sb_uuid);
100         CDEBUG(D_CONFIG, "generated uuid: %s\n", sbi->ll_sb_uuid.uuid);
101
102         sbi->ll_flags |= LL_SBI_VERBOSE;
103         sbi->ll_flags |= LL_SBI_CHECKSUM;
104
105         sbi->ll_flags |= LL_SBI_LRU_RESIZE;
106         sbi->ll_flags |= LL_SBI_LAZYSTATFS;
107
108         for (i = 0; i <= LL_PROCESS_HIST_MAX; i++) {
109                 spin_lock_init(&sbi->ll_rw_extents_info.pp_extents[i].
110                                pp_r_hist.oh_lock);
111                 spin_lock_init(&sbi->ll_rw_extents_info.pp_extents[i].
112                                pp_w_hist.oh_lock);
113         }
114
115         /* metadata statahead is enabled by default */
116         sbi->ll_sa_max = LL_SA_RPC_DEF;
117         atomic_set(&sbi->ll_sa_total, 0);
118         atomic_set(&sbi->ll_sa_wrong, 0);
119         atomic_set(&sbi->ll_sa_running, 0);
120         atomic_set(&sbi->ll_agl_total, 0);
121         sbi->ll_flags |= LL_SBI_AGL_ENABLED;
122
123         /* root squash */
124         sbi->ll_squash.rsi_uid = 0;
125         sbi->ll_squash.rsi_gid = 0;
126         INIT_LIST_HEAD(&sbi->ll_squash.rsi_nosquash_nids);
127         init_rwsem(&sbi->ll_squash.rsi_sem);
128
129         sbi->ll_sb = sb;
130
131         return sbi;
132 }
133
134 static void ll_free_sbi(struct super_block *sb)
135 {
136         struct ll_sb_info *sbi = ll_s2sbi(sb);
137
138         if (sbi->ll_cache) {
139                 if (!list_empty(&sbi->ll_squash.rsi_nosquash_nids))
140                         cfs_free_nidlist(&sbi->ll_squash.rsi_nosquash_nids);
141                 cl_cache_decref(sbi->ll_cache);
142                 sbi->ll_cache = NULL;
143         }
144
145         kfree(sbi);
146 }
147
148 static int client_common_fill_super(struct super_block *sb, char *md, char *dt,
149                                     struct vfsmount *mnt)
150 {
151         struct inode *root = NULL;
152         struct ll_sb_info *sbi = ll_s2sbi(sb);
153         struct obd_device *obd;
154         struct obd_statfs *osfs = NULL;
155         struct ptlrpc_request *request = NULL;
156         struct obd_connect_data *data = NULL;
157         struct obd_uuid *uuid;
158         struct md_op_data *op_data;
159         struct lustre_md lmd;
160         u64 valid;
161         int size, err, checksum;
162
163         obd = class_name2obd(md);
164         if (!obd) {
165                 CERROR("MD %s: not setup or attached\n", md);
166                 return -EINVAL;
167         }
168
169         data = kzalloc(sizeof(*data), GFP_NOFS);
170         if (!data)
171                 return -ENOMEM;
172
173         osfs = kzalloc(sizeof(*osfs), GFP_NOFS);
174         if (!osfs) {
175                 kfree(data);
176                 return -ENOMEM;
177         }
178
179         /* indicate the features supported by this client */
180         data->ocd_connect_flags = OBD_CONNECT_IBITS    | OBD_CONNECT_NODEVOH  |
181                                   OBD_CONNECT_ATTRFID  |
182                                   OBD_CONNECT_VERSION  | OBD_CONNECT_BRW_SIZE |
183                                   OBD_CONNECT_CANCELSET | OBD_CONNECT_FID     |
184                                   OBD_CONNECT_AT       | OBD_CONNECT_LOV_V3   |
185                                   OBD_CONNECT_VBR       | OBD_CONNECT_FULL20  |
186                                   OBD_CONNECT_64BITHASH |
187                                   OBD_CONNECT_EINPROGRESS |
188                                   OBD_CONNECT_JOBSTATS | OBD_CONNECT_LVB_TYPE |
189                                   OBD_CONNECT_LAYOUTLOCK |
190                                   OBD_CONNECT_PINGLESS |
191                                   OBD_CONNECT_MAX_EASIZE |
192                                   OBD_CONNECT_FLOCK_DEAD |
193                                   OBD_CONNECT_DISP_STRIPE | OBD_CONNECT_LFSCK |
194                                   OBD_CONNECT_OPEN_BY_FID |
195                                   OBD_CONNECT_DIR_STRIPE |
196                                   OBD_CONNECT_BULK_MBITS;
197
198         if (sbi->ll_flags & LL_SBI_LRU_RESIZE)
199                 data->ocd_connect_flags |= OBD_CONNECT_LRU_RESIZE;
200 #ifdef CONFIG_FS_POSIX_ACL
201         data->ocd_connect_flags |= OBD_CONNECT_ACL | OBD_CONNECT_UMASK;
202 #endif
203
204         if (OBD_FAIL_CHECK(OBD_FAIL_MDC_LIGHTWEIGHT))
205                 /* flag mdc connection as lightweight, only used for test
206                  * purpose, use with care
207                  */
208                 data->ocd_connect_flags |= OBD_CONNECT_LIGHTWEIGHT;
209
210         data->ocd_ibits_known = MDS_INODELOCK_FULL;
211         data->ocd_version = LUSTRE_VERSION_CODE;
212
213         if (sb_rdonly(sb))
214                 data->ocd_connect_flags |= OBD_CONNECT_RDONLY;
215         if (sbi->ll_flags & LL_SBI_USER_XATTR)
216                 data->ocd_connect_flags |= OBD_CONNECT_XATTR;
217
218         if (sbi->ll_flags & LL_SBI_FLOCK)
219                 sbi->ll_fop = &ll_file_operations_flock;
220         else if (sbi->ll_flags & LL_SBI_LOCALFLOCK)
221                 sbi->ll_fop = &ll_file_operations;
222         else
223                 sbi->ll_fop = &ll_file_operations_noflock;
224
225         /* always ping even if server suppress_pings */
226         if (sbi->ll_flags & LL_SBI_ALWAYS_PING)
227                 data->ocd_connect_flags &= ~OBD_CONNECT_PINGLESS;
228
229         data->ocd_brw_size = MD_MAX_BRW_SIZE;
230
231         err = obd_connect(NULL, &sbi->ll_md_exp, obd, &sbi->ll_sb_uuid,
232                           data, NULL);
233         if (err == -EBUSY) {
234                 LCONSOLE_ERROR_MSG(0x14f, "An MDT (md %s) is performing recovery, of which this client is not a part. Please wait for recovery to complete, abort, or time out.\n",
235                                    md);
236                 goto out;
237         } else if (err) {
238                 CERROR("cannot connect to %s: rc = %d\n", md, err);
239                 goto out;
240         }
241
242         sbi->ll_md_exp->exp_connect_data = *data;
243
244         err = obd_fid_init(sbi->ll_md_exp->exp_obd, sbi->ll_md_exp,
245                            LUSTRE_SEQ_METADATA);
246         if (err) {
247                 CERROR("%s: Can't init metadata layer FID infrastructure, rc = %d\n",
248                        sbi->ll_md_exp->exp_obd->obd_name, err);
249                 goto out_md;
250         }
251
252         /* For mount, we only need fs info from MDT0, and also in DNE, it
253          * can make sure the client can be mounted as long as MDT0 is
254          * available
255          */
256         err = obd_statfs(NULL, sbi->ll_md_exp, osfs,
257                          cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
258                          OBD_STATFS_FOR_MDT0);
259         if (err)
260                 goto out_md_fid;
261
262         /* This needs to be after statfs to ensure connect has finished.
263          * Note that "data" does NOT contain the valid connect reply.
264          * If connecting to a 1.8 server there will be no LMV device, so
265          * we can access the MDC export directly and exp_connect_flags will
266          * be non-zero, but if accessing an upgraded 2.1 server it will
267          * have the correct flags filled in.
268          * XXX: fill in the LMV exp_connect_flags from MDC(s).
269          */
270         valid = exp_connect_flags(sbi->ll_md_exp) & CLIENT_CONNECT_MDT_REQD;
271         if (exp_connect_flags(sbi->ll_md_exp) != 0 &&
272             valid != CLIENT_CONNECT_MDT_REQD) {
273                 char *buf;
274
275                 buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
276                 if (!buf) {
277                         err = -ENOMEM;
278                         goto out_md_fid;
279                 }
280                 obd_connect_flags2str(buf, PAGE_SIZE,
281                                       valid ^ CLIENT_CONNECT_MDT_REQD, ",");
282                 LCONSOLE_ERROR_MSG(0x170, "Server %s does not support feature(s) needed for correct operation of this client (%s). Please upgrade server or downgrade client.\n",
283                                    sbi->ll_md_exp->exp_obd->obd_name, buf);
284                 kfree(buf);
285                 err = -EPROTO;
286                 goto out_md_fid;
287         }
288
289         size = sizeof(*data);
290         err = obd_get_info(NULL, sbi->ll_md_exp, sizeof(KEY_CONN_DATA),
291                            KEY_CONN_DATA,  &size, data);
292         if (err) {
293                 CERROR("%s: Get connect data failed: rc = %d\n",
294                        sbi->ll_md_exp->exp_obd->obd_name, err);
295                 goto out_md_fid;
296         }
297
298         LASSERT(osfs->os_bsize);
299         sb->s_blocksize = osfs->os_bsize;
300         sb->s_blocksize_bits = log2(osfs->os_bsize);
301         sb->s_magic = LL_SUPER_MAGIC;
302         sb->s_maxbytes = MAX_LFS_FILESIZE;
303         sbi->ll_namelen = osfs->os_namelen;
304         sbi->ll_mnt.mnt = current->fs->root.mnt;
305
306         if ((sbi->ll_flags & LL_SBI_USER_XATTR) &&
307             !(data->ocd_connect_flags & OBD_CONNECT_XATTR)) {
308                 LCONSOLE_INFO("Disabling user_xattr feature because it is not supported on the server\n");
309                 sbi->ll_flags &= ~LL_SBI_USER_XATTR;
310         }
311
312         if (data->ocd_connect_flags & OBD_CONNECT_ACL) {
313                 sb->s_flags |= MS_POSIXACL;
314                 sbi->ll_flags |= LL_SBI_ACL;
315         } else {
316                 LCONSOLE_INFO("client wants to enable acl, but mdt not!\n");
317                 sb->s_flags &= ~MS_POSIXACL;
318                 sbi->ll_flags &= ~LL_SBI_ACL;
319         }
320
321         if (data->ocd_connect_flags & OBD_CONNECT_64BITHASH)
322                 sbi->ll_flags |= LL_SBI_64BIT_HASH;
323
324         if (data->ocd_connect_flags & OBD_CONNECT_BRW_SIZE)
325                 sbi->ll_md_brw_pages = data->ocd_brw_size >> PAGE_SHIFT;
326         else
327                 sbi->ll_md_brw_pages = 1;
328
329         if (data->ocd_connect_flags & OBD_CONNECT_LAYOUTLOCK)
330                 sbi->ll_flags |= LL_SBI_LAYOUT_LOCK;
331
332         if (data->ocd_ibits_known & MDS_INODELOCK_XATTR) {
333                 if (!(data->ocd_connect_flags & OBD_CONNECT_MAX_EASIZE)) {
334                         LCONSOLE_INFO(
335                                 "%s: disabling xattr cache due to unknown maximum xattr size.\n",
336                                 dt);
337                 } else {
338                         sbi->ll_flags |= LL_SBI_XATTR_CACHE;
339                         sbi->ll_xattr_cache_enabled = 1;
340                 }
341         }
342
343         obd = class_name2obd(dt);
344         if (!obd) {
345                 CERROR("DT %s: not setup or attached\n", dt);
346                 err = -ENODEV;
347                 goto out_md_fid;
348         }
349
350         data->ocd_connect_flags = OBD_CONNECT_GRANT     | OBD_CONNECT_VERSION  |
351                                   OBD_CONNECT_REQPORTAL | OBD_CONNECT_BRW_SIZE |
352                                   OBD_CONNECT_CANCELSET | OBD_CONNECT_FID      |
353                                   OBD_CONNECT_SRVLOCK   | OBD_CONNECT_TRUNCLOCK|
354                                   OBD_CONNECT_AT        | OBD_CONNECT_OSS_CAPA |
355                                   OBD_CONNECT_VBR       | OBD_CONNECT_FULL20   |
356                                   OBD_CONNECT_64BITHASH | OBD_CONNECT_MAXBYTES |
357                                   OBD_CONNECT_EINPROGRESS |
358                                   OBD_CONNECT_JOBSTATS | OBD_CONNECT_LVB_TYPE |
359                                   OBD_CONNECT_LAYOUTLOCK |
360                                   OBD_CONNECT_PINGLESS | OBD_CONNECT_LFSCK |
361                                   OBD_CONNECT_BULK_MBITS;
362
363         if (!OBD_FAIL_CHECK(OBD_FAIL_OSC_CONNECT_CKSUM)) {
364                 /* OBD_CONNECT_CKSUM should always be set, even if checksums are
365                  * disabled by default, because it can still be enabled on the
366                  * fly via /sys. As a consequence, we still need to come to an
367                  * agreement on the supported algorithms at connect time
368                  */
369                 data->ocd_connect_flags |= OBD_CONNECT_CKSUM;
370
371                 if (OBD_FAIL_CHECK(OBD_FAIL_OSC_CKSUM_ADLER_ONLY))
372                         data->ocd_cksum_types = OBD_CKSUM_ADLER;
373                 else
374                         data->ocd_cksum_types = cksum_types_supported_client();
375         }
376
377         data->ocd_connect_flags |= OBD_CONNECT_LRU_RESIZE;
378
379         /* always ping even if server suppress_pings */
380         if (sbi->ll_flags & LL_SBI_ALWAYS_PING)
381                 data->ocd_connect_flags &= ~OBD_CONNECT_PINGLESS;
382
383         CDEBUG(D_RPCTRACE, "ocd_connect_flags: %#llx ocd_version: %d ocd_grant: %d\n",
384                data->ocd_connect_flags,
385                data->ocd_version, data->ocd_grant);
386
387         obd->obd_upcall.onu_owner = &sbi->ll_lco;
388         obd->obd_upcall.onu_upcall = cl_ocd_update;
389
390         data->ocd_brw_size = DT_MAX_BRW_SIZE;
391
392         err = obd_connect(NULL, &sbi->ll_dt_exp, obd, &sbi->ll_sb_uuid, data,
393                           NULL);
394         if (err == -EBUSY) {
395                 LCONSOLE_ERROR_MSG(0x150, "An OST (dt %s) is performing recovery, of which this client is not a part.  Please wait for recovery to complete, abort, or time out.\n",
396                                    dt);
397                 goto out_md;
398         } else if (err) {
399                 CERROR("%s: Cannot connect to %s: rc = %d\n",
400                        sbi->ll_dt_exp->exp_obd->obd_name, dt, err);
401                 goto out_md;
402         }
403
404         sbi->ll_dt_exp->exp_connect_data = *data;
405
406         err = obd_fid_init(sbi->ll_dt_exp->exp_obd, sbi->ll_dt_exp,
407                            LUSTRE_SEQ_METADATA);
408         if (err) {
409                 CERROR("%s: Can't init data layer FID infrastructure, rc = %d\n",
410                        sbi->ll_dt_exp->exp_obd->obd_name, err);
411                 goto out_dt;
412         }
413
414         mutex_lock(&sbi->ll_lco.lco_lock);
415         sbi->ll_lco.lco_flags = data->ocd_connect_flags;
416         sbi->ll_lco.lco_md_exp = sbi->ll_md_exp;
417         sbi->ll_lco.lco_dt_exp = sbi->ll_dt_exp;
418         mutex_unlock(&sbi->ll_lco.lco_lock);
419
420         fid_zero(&sbi->ll_root_fid);
421         err = md_getstatus(sbi->ll_md_exp, &sbi->ll_root_fid);
422         if (err) {
423                 CERROR("cannot mds_connect: rc = %d\n", err);
424                 goto out_lock_cn_cb;
425         }
426         if (!fid_is_sane(&sbi->ll_root_fid)) {
427                 CERROR("%s: Invalid root fid " DFID " during mount\n",
428                        sbi->ll_md_exp->exp_obd->obd_name,
429                        PFID(&sbi->ll_root_fid));
430                 err = -EINVAL;
431                 goto out_lock_cn_cb;
432         }
433         CDEBUG(D_SUPER, "rootfid " DFID "\n", PFID(&sbi->ll_root_fid));
434
435         sb->s_op = &lustre_super_operations;
436         sb->s_xattr = ll_xattr_handlers;
437 #if THREAD_SIZE >= 8192 /*b=17630*/
438         sb->s_export_op = &lustre_export_operations;
439 #endif
440
441         /* make root inode
442          * XXX: move this to after cbd setup?
443          */
444         valid = OBD_MD_FLGETATTR | OBD_MD_FLBLOCKS | OBD_MD_FLMODEASIZE;
445         if (sbi->ll_flags & LL_SBI_ACL)
446                 valid |= OBD_MD_FLACL;
447
448         op_data = kzalloc(sizeof(*op_data), GFP_NOFS);
449         if (!op_data) {
450                 err = -ENOMEM;
451                 goto out_lock_cn_cb;
452         }
453
454         op_data->op_fid1 = sbi->ll_root_fid;
455         op_data->op_mode = 0;
456         op_data->op_valid = valid;
457
458         err = md_getattr(sbi->ll_md_exp, op_data, &request);
459         kfree(op_data);
460         if (err) {
461                 CERROR("%s: md_getattr failed for root: rc = %d\n",
462                        sbi->ll_md_exp->exp_obd->obd_name, err);
463                 goto out_lock_cn_cb;
464         }
465
466         err = md_get_lustre_md(sbi->ll_md_exp, request, sbi->ll_dt_exp,
467                                sbi->ll_md_exp, &lmd);
468         if (err) {
469                 CERROR("failed to understand root inode md: rc = %d\n", err);
470                 ptlrpc_req_finished(request);
471                 goto out_lock_cn_cb;
472         }
473
474         LASSERT(fid_is_sane(&sbi->ll_root_fid));
475         root = ll_iget(sb, cl_fid_build_ino(&sbi->ll_root_fid,
476                                             sbi->ll_flags & LL_SBI_32BIT_API),
477                        &lmd);
478         md_free_lustre_md(sbi->ll_md_exp, &lmd);
479         ptlrpc_req_finished(request);
480
481         if (IS_ERR(root)) {
482 #ifdef CONFIG_FS_POSIX_ACL
483                 if (lmd.posix_acl) {
484                         posix_acl_release(lmd.posix_acl);
485                         lmd.posix_acl = NULL;
486                 }
487 #endif
488                 err = -EBADF;
489                 CERROR("lustre_lite: bad iget4 for root\n");
490                 goto out_root;
491         }
492
493         checksum = sbi->ll_flags & LL_SBI_CHECKSUM;
494         err = obd_set_info_async(NULL, sbi->ll_dt_exp, sizeof(KEY_CHECKSUM),
495                                  KEY_CHECKSUM, sizeof(checksum), &checksum,
496                                  NULL);
497         if (err) {
498                 CERROR("%s: Set checksum failed: rc = %d\n",
499                        sbi->ll_dt_exp->exp_obd->obd_name, err);
500                 goto out_root;
501         }
502         cl_sb_init(sb);
503
504         err = obd_set_info_async(NULL, sbi->ll_dt_exp, sizeof(KEY_CACHE_SET),
505                                  KEY_CACHE_SET, sizeof(*sbi->ll_cache),
506                                  sbi->ll_cache, NULL);
507         if (err) {
508                 CERROR("%s: Set cache_set failed: rc = %d\n",
509                        sbi->ll_dt_exp->exp_obd->obd_name, err);
510                 goto out_root;
511         }
512
513         sb->s_root = d_make_root(root);
514         if (!sb->s_root) {
515                 CERROR("%s: can't make root dentry\n",
516                        ll_get_fsname(sb, NULL, 0));
517                 err = -ENOMEM;
518                 goto out_lock_cn_cb;
519         }
520
521         sbi->ll_sdev_orig = sb->s_dev;
522
523         /* We set sb->s_dev equal on all lustre clients in order to support
524          * NFS export clustering.  NFSD requires that the FSID be the same
525          * on all clients.
526          */
527         /* s_dev is also used in lt_compare() to compare two fs, but that is
528          * only a node-local comparison.
529          */
530         uuid = obd_get_uuid(sbi->ll_md_exp);
531         if (uuid) {
532                 sb->s_dev = get_uuid2int(uuid->uuid, strlen(uuid->uuid));
533                 get_uuid2fsid(uuid->uuid, strlen(uuid->uuid), &sbi->ll_fsid);
534         }
535
536         kfree(data);
537         kfree(osfs);
538
539         if (llite_root) {
540                 err = ldebugfs_register_mountpoint(llite_root, sb, dt, md);
541                 if (err < 0) {
542                         CERROR("%s: could not register mount in debugfs: "
543                                "rc = %d\n", ll_get_fsname(sb, NULL, 0), err);
544                         err = 0;
545                 }
546         }
547
548         return err;
549 out_root:
550         iput(root);
551 out_lock_cn_cb:
552         obd_fid_fini(sbi->ll_dt_exp->exp_obd);
553 out_dt:
554         obd_disconnect(sbi->ll_dt_exp);
555         sbi->ll_dt_exp = NULL;
556 out_md_fid:
557         obd_fid_fini(sbi->ll_md_exp->exp_obd);
558 out_md:
559         obd_disconnect(sbi->ll_md_exp);
560         sbi->ll_md_exp = NULL;
561 out:
562         kfree(data);
563         kfree(osfs);
564         return err;
565 }
566
567 int ll_get_max_mdsize(struct ll_sb_info *sbi, int *lmmsize)
568 {
569         int size, rc;
570
571         size = sizeof(*lmmsize);
572         rc = obd_get_info(NULL, sbi->ll_dt_exp, sizeof(KEY_MAX_EASIZE),
573                           KEY_MAX_EASIZE, &size, lmmsize);
574         if (rc) {
575                 CERROR("%s: cannot get max LOV EA size: rc = %d\n",
576                        sbi->ll_dt_exp->exp_obd->obd_name, rc);
577                 return rc;
578         }
579
580         size = sizeof(int);
581         rc = obd_get_info(NULL, sbi->ll_md_exp, sizeof(KEY_MAX_EASIZE),
582                           KEY_MAX_EASIZE, &size, lmmsize);
583         if (rc)
584                 CERROR("Get max mdsize error rc %d\n", rc);
585
586         return rc;
587 }
588
589 /**
590  * Get the value of the default_easize parameter.
591  *
592  * \see client_obd::cl_default_mds_easize
593  *
594  * \param[in]  sbi      superblock info for this filesystem
595  * \param[out] lmmsize  pointer to storage location for value
596  *
597  * \retval 0            on success
598  * \retval negative     negated errno on failure
599  */
600 int ll_get_default_mdsize(struct ll_sb_info *sbi, int *lmmsize)
601 {
602         int size, rc;
603
604         size = sizeof(int);
605         rc = obd_get_info(NULL, sbi->ll_md_exp, sizeof(KEY_DEFAULT_EASIZE),
606                           KEY_DEFAULT_EASIZE, &size, lmmsize);
607         if (rc)
608                 CERROR("Get default mdsize error rc %d\n", rc);
609
610         return rc;
611 }
612
613 /**
614  * Set the default_easize parameter to the given value.
615  *
616  * \see client_obd::cl_default_mds_easize
617  *
618  * \param[in] sbi       superblock info for this filesystem
619  * \param[in] lmmsize   the size to set
620  *
621  * \retval 0            on success
622  * \retval negative     negated errno on failure
623  */
624 int ll_set_default_mdsize(struct ll_sb_info *sbi, int lmmsize)
625 {
626         if (lmmsize < sizeof(struct lov_mds_md) ||
627             lmmsize > OBD_MAX_DEFAULT_EA_SIZE)
628                 return -EINVAL;
629
630         return obd_set_info_async(NULL, sbi->ll_md_exp,
631                                   sizeof(KEY_DEFAULT_EASIZE),
632                                   KEY_DEFAULT_EASIZE,
633                                   sizeof(int), &lmmsize, NULL);
634 }
635
636 static void client_common_put_super(struct super_block *sb)
637 {
638         struct ll_sb_info *sbi = ll_s2sbi(sb);
639
640         cl_sb_fini(sb);
641
642         obd_fid_fini(sbi->ll_dt_exp->exp_obd);
643         obd_disconnect(sbi->ll_dt_exp);
644         sbi->ll_dt_exp = NULL;
645
646         ldebugfs_unregister_mountpoint(sbi);
647
648         obd_fid_fini(sbi->ll_md_exp->exp_obd);
649         obd_disconnect(sbi->ll_md_exp);
650         sbi->ll_md_exp = NULL;
651 }
652
653 void ll_kill_super(struct super_block *sb)
654 {
655         struct ll_sb_info *sbi;
656
657         /* not init sb ?*/
658         if (!(sb->s_flags & MS_ACTIVE))
659                 return;
660
661         sbi = ll_s2sbi(sb);
662         /* we need to restore s_dev from changed for clustered NFS before
663          * put_super because new kernels have cached s_dev and change sb->s_dev
664          * in put_super not affected real removing devices
665          */
666         if (sbi) {
667                 sb->s_dev = sbi->ll_sdev_orig;
668                 sbi->ll_umounting = 1;
669
670                 /* wait running statahead threads to quit */
671                 while (atomic_read(&sbi->ll_sa_running) > 0) {
672                         set_current_state(TASK_UNINTERRUPTIBLE);
673                         schedule_timeout(msecs_to_jiffies(MSEC_PER_SEC >> 3));
674                 }
675         }
676 }
677
678 static inline int ll_set_opt(const char *opt, char *data, int fl)
679 {
680         if (strncmp(opt, data, strlen(opt)) != 0)
681                 return 0;
682         else
683                 return fl;
684 }
685
686 /* non-client-specific mount options are parsed in lmd_parse */
687 static int ll_options(char *options, int *flags)
688 {
689         int tmp;
690         char *s1 = options, *s2;
691
692         if (!options)
693                 return 0;
694
695         CDEBUG(D_CONFIG, "Parsing opts %s\n", options);
696
697         while (*s1) {
698                 CDEBUG(D_SUPER, "next opt=%s\n", s1);
699                 tmp = ll_set_opt("nolock", s1, LL_SBI_NOLCK);
700                 if (tmp) {
701                         *flags |= tmp;
702                         goto next;
703                 }
704                 tmp = ll_set_opt("flock", s1, LL_SBI_FLOCK);
705                 if (tmp) {
706                         *flags |= tmp;
707                         goto next;
708                 }
709                 tmp = ll_set_opt("localflock", s1, LL_SBI_LOCALFLOCK);
710                 if (tmp) {
711                         *flags |= tmp;
712                         goto next;
713                 }
714                 tmp = ll_set_opt("noflock", s1,
715                                  LL_SBI_FLOCK | LL_SBI_LOCALFLOCK);
716                 if (tmp) {
717                         *flags &= ~tmp;
718                         goto next;
719                 }
720                 tmp = ll_set_opt("user_xattr", s1, LL_SBI_USER_XATTR);
721                 if (tmp) {
722                         *flags |= tmp;
723                         goto next;
724                 }
725                 tmp = ll_set_opt("nouser_xattr", s1, LL_SBI_USER_XATTR);
726                 if (tmp) {
727                         *flags &= ~tmp;
728                         goto next;
729                 }
730                 tmp = ll_set_opt("context", s1, 1);
731                 if (tmp)
732                         goto next;
733                 tmp = ll_set_opt("fscontext", s1, 1);
734                 if (tmp)
735                         goto next;
736                 tmp = ll_set_opt("defcontext", s1, 1);
737                 if (tmp)
738                         goto next;
739                 tmp = ll_set_opt("rootcontext", s1, 1);
740                 if (tmp)
741                         goto next;
742                 tmp = ll_set_opt("user_fid2path", s1, LL_SBI_USER_FID2PATH);
743                 if (tmp) {
744                         *flags |= tmp;
745                         goto next;
746                 }
747                 tmp = ll_set_opt("nouser_fid2path", s1, LL_SBI_USER_FID2PATH);
748                 if (tmp) {
749                         *flags &= ~tmp;
750                         goto next;
751                 }
752
753                 tmp = ll_set_opt("checksum", s1, LL_SBI_CHECKSUM);
754                 if (tmp) {
755                         *flags |= tmp;
756                         goto next;
757                 }
758                 tmp = ll_set_opt("nochecksum", s1, LL_SBI_CHECKSUM);
759                 if (tmp) {
760                         *flags &= ~tmp;
761                         goto next;
762                 }
763                 tmp = ll_set_opt("lruresize", s1, LL_SBI_LRU_RESIZE);
764                 if (tmp) {
765                         *flags |= tmp;
766                         goto next;
767                 }
768                 tmp = ll_set_opt("nolruresize", s1, LL_SBI_LRU_RESIZE);
769                 if (tmp) {
770                         *flags &= ~tmp;
771                         goto next;
772                 }
773                 tmp = ll_set_opt("lazystatfs", s1, LL_SBI_LAZYSTATFS);
774                 if (tmp) {
775                         *flags |= tmp;
776                         goto next;
777                 }
778                 tmp = ll_set_opt("nolazystatfs", s1, LL_SBI_LAZYSTATFS);
779                 if (tmp) {
780                         *flags &= ~tmp;
781                         goto next;
782                 }
783                 tmp = ll_set_opt("32bitapi", s1, LL_SBI_32BIT_API);
784                 if (tmp) {
785                         *flags |= tmp;
786                         goto next;
787                 }
788                 tmp = ll_set_opt("verbose", s1, LL_SBI_VERBOSE);
789                 if (tmp) {
790                         *flags |= tmp;
791                         goto next;
792                 }
793                 tmp = ll_set_opt("noverbose", s1, LL_SBI_VERBOSE);
794                 if (tmp) {
795                         *flags &= ~tmp;
796                         goto next;
797                 }
798                 tmp = ll_set_opt("always_ping", s1, LL_SBI_ALWAYS_PING);
799                 if (tmp) {
800                         *flags |= tmp;
801                         goto next;
802                 }
803                 LCONSOLE_ERROR_MSG(0x152, "Unknown option '%s', won't mount.\n",
804                                    s1);
805                 return -EINVAL;
806
807 next:
808                 /* Find next opt */
809                 s2 = strchr(s1, ',');
810                 if (!s2)
811                         break;
812                 s1 = s2 + 1;
813         }
814         return 0;
815 }
816
817 void ll_lli_init(struct ll_inode_info *lli)
818 {
819         lli->lli_inode_magic = LLI_INODE_MAGIC;
820         lli->lli_flags = 0;
821         spin_lock_init(&lli->lli_lock);
822         lli->lli_posix_acl = NULL;
823         /* Do not set lli_fid, it has been initialized already. */
824         fid_zero(&lli->lli_pfid);
825         lli->lli_mds_read_och = NULL;
826         lli->lli_mds_write_och = NULL;
827         lli->lli_mds_exec_och = NULL;
828         lli->lli_open_fd_read_count = 0;
829         lli->lli_open_fd_write_count = 0;
830         lli->lli_open_fd_exec_count = 0;
831         mutex_init(&lli->lli_och_mutex);
832         spin_lock_init(&lli->lli_agl_lock);
833         spin_lock_init(&lli->lli_layout_lock);
834         ll_layout_version_set(lli, CL_LAYOUT_GEN_NONE);
835         lli->lli_clob = NULL;
836
837         init_rwsem(&lli->lli_xattrs_list_rwsem);
838         mutex_init(&lli->lli_xattrs_enq_lock);
839
840         LASSERT(lli->lli_vfs_inode.i_mode != 0);
841         if (S_ISDIR(lli->lli_vfs_inode.i_mode)) {
842                 mutex_init(&lli->lli_readdir_mutex);
843                 lli->lli_opendir_key = NULL;
844                 lli->lli_sai = NULL;
845                 spin_lock_init(&lli->lli_sa_lock);
846                 lli->lli_opendir_pid = 0;
847                 lli->lli_sa_enabled = 0;
848                 lli->lli_def_stripe_offset = -1;
849         } else {
850                 mutex_init(&lli->lli_size_mutex);
851                 lli->lli_symlink_name = NULL;
852                 init_rwsem(&lli->lli_trunc_sem);
853                 range_lock_tree_init(&lli->lli_write_tree);
854                 init_rwsem(&lli->lli_glimpse_sem);
855                 lli->lli_glimpse_time = 0;
856                 INIT_LIST_HEAD(&lli->lli_agl_list);
857                 lli->lli_agl_index = 0;
858                 lli->lli_async_rc = 0;
859         }
860         mutex_init(&lli->lli_layout_mutex);
861 }
862
863 int ll_fill_super(struct super_block *sb, struct vfsmount *mnt)
864 {
865         struct lustre_profile *lprof = NULL;
866         struct lustre_sb_info *lsi = s2lsi(sb);
867         struct ll_sb_info *sbi;
868         char  *dt = NULL, *md = NULL;
869         char  *profilenm = get_profile_name(sb);
870         struct config_llog_instance *cfg;
871         int    err;
872         static atomic_t ll_bdi_num = ATOMIC_INIT(0);
873
874         CDEBUG(D_VFSTRACE, "VFS Op: sb %p\n", sb);
875
876         cfg = kzalloc(sizeof(*cfg), GFP_NOFS);
877         if (!cfg)
878                 return -ENOMEM;
879
880         try_module_get(THIS_MODULE);
881
882         /* client additional sb info */
883         sbi = ll_init_sbi(sb);
884         lsi->lsi_llsbi = sbi;
885         if (!sbi) {
886                 module_put(THIS_MODULE);
887                 kfree(cfg);
888                 return -ENOMEM;
889         }
890
891         err = ll_options(lsi->lsi_lmd->lmd_opts, &sbi->ll_flags);
892         if (err)
893                 goto out_free;
894
895         err = super_setup_bdi_name(sb, "lustre-%d",
896                                    atomic_inc_return(&ll_bdi_num));
897         if (err)
898                 goto out_free;
899
900         /* kernel >= 2.6.38 store dentry operations in sb->s_d_op. */
901         sb->s_d_op = &ll_d_ops;
902
903         /* Generate a string unique to this super, in case some joker tries
904          * to mount the same fs at two mount points.
905          * Use the address of the super itself.
906          */
907         cfg->cfg_instance = sb;
908         cfg->cfg_uuid = lsi->lsi_llsbi->ll_sb_uuid;
909         cfg->cfg_callback = class_config_llog_handler;
910         /* set up client obds */
911         err = lustre_process_log(sb, profilenm, cfg);
912         if (err < 0)
913                 goto out_free;
914
915         /* Profile set with LCFG_MOUNTOPT so we can find our mdc and osc obds */
916         lprof = class_get_profile(profilenm);
917         if (!lprof) {
918                 LCONSOLE_ERROR_MSG(0x156, "The client profile '%s' could not be read from the MGS.  Does that filesystem exist?\n",
919                                    profilenm);
920                 err = -EINVAL;
921                 goto out_free;
922         }
923         CDEBUG(D_CONFIG, "Found profile %s: mdc=%s osc=%s\n", profilenm,
924                lprof->lp_md, lprof->lp_dt);
925
926         dt = kasprintf(GFP_NOFS, "%s-%p", lprof->lp_dt, cfg->cfg_instance);
927         if (!dt) {
928                 err = -ENOMEM;
929                 goto out_free;
930         }
931
932         md = kasprintf(GFP_NOFS, "%s-%p", lprof->lp_md, cfg->cfg_instance);
933         if (!md) {
934                 err = -ENOMEM;
935                 goto out_free;
936         }
937
938         /* connections, registrations, sb setup */
939         err = client_common_fill_super(sb, md, dt, mnt);
940         if (!err)
941                 sbi->ll_client_common_fill_super_succeeded = 1;
942
943 out_free:
944         kfree(md);
945         kfree(dt);
946         if (lprof)
947                 class_put_profile(lprof);
948         if (err)
949                 ll_put_super(sb);
950         else if (sbi->ll_flags & LL_SBI_VERBOSE)
951                 LCONSOLE_WARN("Mounted %s\n", profilenm);
952
953         kfree(cfg);
954         return err;
955 } /* ll_fill_super */
956
957 void ll_put_super(struct super_block *sb)
958 {
959         struct config_llog_instance cfg, params_cfg;
960         struct obd_device *obd;
961         struct lustre_sb_info *lsi = s2lsi(sb);
962         struct ll_sb_info *sbi = ll_s2sbi(sb);
963         char *profilenm = get_profile_name(sb);
964         int next, force = 1, rc = 0;
965         long ccc_count;
966
967         CDEBUG(D_VFSTRACE, "VFS Op: sb %p - %s\n", sb, profilenm);
968
969         cfg.cfg_instance = sb;
970         lustre_end_log(sb, profilenm, &cfg);
971
972         params_cfg.cfg_instance = sb;
973         lustre_end_log(sb, PARAMS_FILENAME, &params_cfg);
974
975         if (sbi->ll_md_exp) {
976                 obd = class_exp2obd(sbi->ll_md_exp);
977                 if (obd)
978                         force = obd->obd_force;
979         }
980
981         /* Wait for unstable pages to be committed to stable storage */
982         if (!force) {
983                 struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
984
985                 rc = l_wait_event(sbi->ll_cache->ccc_unstable_waitq,
986                                   !atomic_long_read(&sbi->ll_cache->ccc_unstable_nr),
987                                   &lwi);
988         }
989
990         ccc_count = atomic_long_read(&sbi->ll_cache->ccc_unstable_nr);
991         if (!force && rc != -EINTR)
992                 LASSERTF(!ccc_count, "count: %li\n", ccc_count);
993
994         /* We need to set force before the lov_disconnect in
995          * lustre_common_put_super, since l_d cleans up osc's as well.
996          */
997         if (force) {
998                 next = 0;
999                 while ((obd = class_devices_in_group(&sbi->ll_sb_uuid,
1000                                                      &next)) != NULL) {
1001                         obd->obd_force = force;
1002                 }
1003         }
1004
1005         if (sbi->ll_client_common_fill_super_succeeded) {
1006                 /* Only if client_common_fill_super succeeded */
1007                 client_common_put_super(sb);
1008         }
1009
1010         next = 0;
1011         while ((obd = class_devices_in_group(&sbi->ll_sb_uuid, &next)))
1012                 class_manual_cleanup(obd);
1013
1014         if (sbi->ll_flags & LL_SBI_VERBOSE)
1015                 LCONSOLE_WARN("Unmounted %s\n", profilenm ? profilenm : "");
1016
1017         if (profilenm)
1018                 class_del_profile(profilenm);
1019
1020         ll_free_sbi(sb);
1021         lsi->lsi_llsbi = NULL;
1022
1023         lustre_common_put_super(sb);
1024
1025         cl_env_cache_purge(~0);
1026
1027         module_put(THIS_MODULE);
1028 } /* client_put_super */
1029
1030 struct inode *ll_inode_from_resource_lock(struct ldlm_lock *lock)
1031 {
1032         struct inode *inode = NULL;
1033
1034         /* NOTE: we depend on atomic igrab() -bzzz */
1035         lock_res_and_lock(lock);
1036         if (lock->l_resource->lr_lvb_inode) {
1037                 struct ll_inode_info *lli;
1038
1039                 lli = ll_i2info(lock->l_resource->lr_lvb_inode);
1040                 if (lli->lli_inode_magic == LLI_INODE_MAGIC) {
1041                         inode = igrab(lock->l_resource->lr_lvb_inode);
1042                 } else {
1043                         inode = lock->l_resource->lr_lvb_inode;
1044                         LDLM_DEBUG_LIMIT(inode->i_state & I_FREEING ?  D_INFO :
1045                                          D_WARNING, lock, "lr_lvb_inode %p is bogus: magic %08x",
1046                                          lock->l_resource->lr_lvb_inode,
1047                                          lli->lli_inode_magic);
1048                         inode = NULL;
1049                 }
1050         }
1051         unlock_res_and_lock(lock);
1052         return inode;
1053 }
1054
1055 void ll_dir_clear_lsm_md(struct inode *inode)
1056 {
1057         struct ll_inode_info *lli = ll_i2info(inode);
1058
1059         LASSERT(S_ISDIR(inode->i_mode));
1060
1061         if (lli->lli_lsm_md) {
1062                 lmv_free_memmd(lli->lli_lsm_md);
1063                 lli->lli_lsm_md = NULL;
1064         }
1065 }
1066
1067 static struct inode *ll_iget_anon_dir(struct super_block *sb,
1068                                       const struct lu_fid *fid,
1069                                       struct lustre_md *md)
1070 {
1071         struct ll_sb_info *sbi = ll_s2sbi(sb);
1072         struct mdt_body *body = md->body;
1073         struct inode *inode;
1074         ino_t ino;
1075
1076         ino = cl_fid_build_ino(fid, sbi->ll_flags & LL_SBI_32BIT_API);
1077         inode = iget_locked(sb, ino);
1078         if (!inode) {
1079                 CERROR("%s: failed get simple inode " DFID ": rc = -ENOENT\n",
1080                        ll_get_fsname(sb, NULL, 0), PFID(fid));
1081                 return ERR_PTR(-ENOENT);
1082         }
1083
1084         if (inode->i_state & I_NEW) {
1085                 struct ll_inode_info *lli = ll_i2info(inode);
1086                 struct lmv_stripe_md *lsm = md->lmv;
1087
1088                 inode->i_mode = (inode->i_mode & ~S_IFMT) |
1089                                 (body->mbo_mode & S_IFMT);
1090                 LASSERTF(S_ISDIR(inode->i_mode), "Not slave inode " DFID "\n",
1091                          PFID(fid));
1092
1093                 LTIME_S(inode->i_mtime) = 0;
1094                 LTIME_S(inode->i_atime) = 0;
1095                 LTIME_S(inode->i_ctime) = 0;
1096                 inode->i_rdev = 0;
1097
1098                 inode->i_op = &ll_dir_inode_operations;
1099                 inode->i_fop = &ll_dir_operations;
1100                 lli->lli_fid = *fid;
1101                 ll_lli_init(lli);
1102
1103                 LASSERT(lsm);
1104                 /* master object FID */
1105                 lli->lli_pfid = body->mbo_fid1;
1106                 CDEBUG(D_INODE, "lli %p slave " DFID " master " DFID "\n",
1107                        lli, PFID(fid), PFID(&lli->lli_pfid));
1108                 unlock_new_inode(inode);
1109         }
1110
1111         return inode;
1112 }
1113
1114 static int ll_init_lsm_md(struct inode *inode, struct lustre_md *md)
1115 {
1116         struct lmv_stripe_md *lsm = md->lmv;
1117         struct lu_fid *fid;
1118         int i;
1119
1120         LASSERT(lsm);
1121         /*
1122          * XXX sigh, this lsm_root initialization should be in
1123          * LMV layer, but it needs ll_iget right now, so we
1124          * put this here right now.
1125          */
1126         for (i = 0; i < lsm->lsm_md_stripe_count; i++) {
1127                 fid = &lsm->lsm_md_oinfo[i].lmo_fid;
1128                 LASSERT(!lsm->lsm_md_oinfo[i].lmo_root);
1129                 /* Unfortunately ll_iget will call ll_update_inode,
1130                  * where the initialization of slave inode is slightly
1131                  * different, so it reset lsm_md to NULL to avoid
1132                  * initializing lsm for slave inode.
1133                  */
1134                 /* For migrating inode, master stripe and master object will
1135                  * be same, so we only need assign this inode
1136                  */
1137                 if (lsm->lsm_md_hash_type & LMV_HASH_FLAG_MIGRATION && !i)
1138                         lsm->lsm_md_oinfo[i].lmo_root = inode;
1139                 else
1140                         lsm->lsm_md_oinfo[i].lmo_root =
1141                                 ll_iget_anon_dir(inode->i_sb, fid, md);
1142                 if (IS_ERR(lsm->lsm_md_oinfo[i].lmo_root)) {
1143                         int rc = PTR_ERR(lsm->lsm_md_oinfo[i].lmo_root);
1144
1145                         lsm->lsm_md_oinfo[i].lmo_root = NULL;
1146                         return rc;
1147                 }
1148         }
1149
1150         return 0;
1151 }
1152
1153 static inline int lli_lsm_md_eq(const struct lmv_stripe_md *lsm_md1,
1154                                 const struct lmv_stripe_md *lsm_md2)
1155 {
1156         return lsm_md1->lsm_md_magic == lsm_md2->lsm_md_magic &&
1157                lsm_md1->lsm_md_stripe_count == lsm_md2->lsm_md_stripe_count &&
1158                lsm_md1->lsm_md_master_mdt_index ==
1159                         lsm_md2->lsm_md_master_mdt_index &&
1160                lsm_md1->lsm_md_hash_type == lsm_md2->lsm_md_hash_type &&
1161                lsm_md1->lsm_md_layout_version ==
1162                         lsm_md2->lsm_md_layout_version &&
1163                !strcmp(lsm_md1->lsm_md_pool_name,
1164                        lsm_md2->lsm_md_pool_name);
1165 }
1166
1167 static int ll_update_lsm_md(struct inode *inode, struct lustre_md *md)
1168 {
1169         struct ll_inode_info *lli = ll_i2info(inode);
1170         struct lmv_stripe_md *lsm = md->lmv;
1171         int rc;
1172
1173         LASSERT(S_ISDIR(inode->i_mode));
1174         CDEBUG(D_INODE, "update lsm %p of " DFID "\n", lli->lli_lsm_md,
1175                PFID(ll_inode2fid(inode)));
1176
1177         /* no striped information from request. */
1178         if (!lsm) {
1179                 if (!lli->lli_lsm_md) {
1180                         return 0;
1181                 } else if (lli->lli_lsm_md->lsm_md_hash_type &
1182                            LMV_HASH_FLAG_MIGRATION) {
1183                         /*
1184                          * migration is done, the temporay MIGRATE layout has
1185                          * been removed
1186                          */
1187                         CDEBUG(D_INODE, DFID " finish migration.\n",
1188                                PFID(ll_inode2fid(inode)));
1189                         lmv_free_memmd(lli->lli_lsm_md);
1190                         lli->lli_lsm_md = NULL;
1191                         return 0;
1192                 } else {
1193                         /*
1194                          * The lustre_md from req does not include stripeEA,
1195                          * see ll_md_setattr
1196                          */
1197                         return 0;
1198                 }
1199         }
1200
1201         /* set the directory layout */
1202         if (!lli->lli_lsm_md) {
1203                 struct cl_attr *attr;
1204
1205                 rc = ll_init_lsm_md(inode, md);
1206                 if (rc)
1207                         return rc;
1208
1209                 /*
1210                  * set lsm_md to NULL, so the following free lustre_md
1211                  * will not free this lsm
1212                  */
1213                 md->lmv = NULL;
1214                 lli->lli_lsm_md = lsm;
1215
1216                 attr = kzalloc(sizeof(*attr), GFP_NOFS);
1217                 if (!attr)
1218                         return -ENOMEM;
1219
1220                 /* validate the lsm */
1221                 rc = md_merge_attr(ll_i2mdexp(inode), lsm, attr,
1222                                    ll_md_blocking_ast);
1223                 if (rc) {
1224                         kfree(attr);
1225                         return rc;
1226                 }
1227
1228                 if (md->body->mbo_valid & OBD_MD_FLNLINK)
1229                         md->body->mbo_nlink = attr->cat_nlink;
1230                 if (md->body->mbo_valid & OBD_MD_FLSIZE)
1231                         md->body->mbo_size = attr->cat_size;
1232                 if (md->body->mbo_valid & OBD_MD_FLATIME)
1233                         md->body->mbo_atime = attr->cat_atime;
1234                 if (md->body->mbo_valid & OBD_MD_FLCTIME)
1235                         md->body->mbo_ctime = attr->cat_ctime;
1236                 if (md->body->mbo_valid & OBD_MD_FLMTIME)
1237                         md->body->mbo_mtime = attr->cat_mtime;
1238
1239                 kfree(attr);
1240
1241                 CDEBUG(D_INODE, "Set lsm %p magic %x to " DFID "\n", lsm,
1242                        lsm->lsm_md_magic, PFID(ll_inode2fid(inode)));
1243                 return 0;
1244         }
1245
1246         /* Compare the old and new stripe information */
1247         if (!lsm_md_eq(lli->lli_lsm_md, lsm)) {
1248                 struct lmv_stripe_md *old_lsm = lli->lli_lsm_md;
1249                 int idx;
1250
1251                 CERROR("%s: inode " DFID "(%p)'s lmv layout mismatch (%p)/(%p) magic:0x%x/0x%x stripe count: %d/%d master_mdt: %d/%d hash_type:0x%x/0x%x layout: 0x%x/0x%x pool:%s/%s\n",
1252                        ll_get_fsname(inode->i_sb, NULL, 0), PFID(&lli->lli_fid),
1253                        inode, lsm, old_lsm,
1254                        lsm->lsm_md_magic, old_lsm->lsm_md_magic,
1255                        lsm->lsm_md_stripe_count,
1256                        old_lsm->lsm_md_stripe_count,
1257                        lsm->lsm_md_master_mdt_index,
1258                        old_lsm->lsm_md_master_mdt_index,
1259                        lsm->lsm_md_hash_type, old_lsm->lsm_md_hash_type,
1260                        lsm->lsm_md_layout_version,
1261                        old_lsm->lsm_md_layout_version,
1262                        lsm->lsm_md_pool_name,
1263                        old_lsm->lsm_md_pool_name);
1264
1265                 for (idx = 0; idx < old_lsm->lsm_md_stripe_count; idx++) {
1266                         CERROR("%s: sub FIDs in old lsm idx %d, old: " DFID "\n",
1267                                ll_get_fsname(inode->i_sb, NULL, 0), idx,
1268                                PFID(&old_lsm->lsm_md_oinfo[idx].lmo_fid));
1269                 }
1270
1271                 for (idx = 0; idx < lsm->lsm_md_stripe_count; idx++) {
1272                         CERROR("%s: sub FIDs in new lsm idx %d, new: " DFID "\n",
1273                                ll_get_fsname(inode->i_sb, NULL, 0), idx,
1274                                PFID(&lsm->lsm_md_oinfo[idx].lmo_fid));
1275                 }
1276
1277                 return -EIO;
1278         }
1279
1280         return 0;
1281 }
1282
1283 void ll_clear_inode(struct inode *inode)
1284 {
1285         struct ll_inode_info *lli = ll_i2info(inode);
1286         struct ll_sb_info *sbi = ll_i2sbi(inode);
1287
1288         CDEBUG(D_VFSTRACE, "VFS Op:inode=" DFID "(%p)\n",
1289                PFID(ll_inode2fid(inode)), inode);
1290
1291         if (S_ISDIR(inode->i_mode)) {
1292                 /* these should have been cleared in ll_file_release */
1293                 LASSERT(!lli->lli_opendir_key);
1294                 LASSERT(!lli->lli_sai);
1295                 LASSERT(lli->lli_opendir_pid == 0);
1296         }
1297
1298         md_null_inode(sbi->ll_md_exp, ll_inode2fid(inode));
1299
1300         LASSERT(!lli->lli_open_fd_write_count);
1301         LASSERT(!lli->lli_open_fd_read_count);
1302         LASSERT(!lli->lli_open_fd_exec_count);
1303
1304         if (lli->lli_mds_write_och)
1305                 ll_md_real_close(inode, FMODE_WRITE);
1306         if (lli->lli_mds_exec_och)
1307                 ll_md_real_close(inode, FMODE_EXEC);
1308         if (lli->lli_mds_read_och)
1309                 ll_md_real_close(inode, FMODE_READ);
1310
1311         if (S_ISLNK(inode->i_mode)) {
1312                 kfree(lli->lli_symlink_name);
1313                 lli->lli_symlink_name = NULL;
1314         }
1315
1316         ll_xattr_cache_destroy(inode);
1317
1318 #ifdef CONFIG_FS_POSIX_ACL
1319         forget_all_cached_acls(inode);
1320         if (lli->lli_posix_acl) {
1321                 posix_acl_release(lli->lli_posix_acl);
1322                 lli->lli_posix_acl = NULL;
1323         }
1324 #endif
1325         lli->lli_inode_magic = LLI_INODE_DEAD;
1326
1327         if (S_ISDIR(inode->i_mode))
1328                 ll_dir_clear_lsm_md(inode);
1329         if (S_ISREG(inode->i_mode) && !is_bad_inode(inode))
1330                 LASSERT(list_empty(&lli->lli_agl_list));
1331
1332         /*
1333          * XXX This has to be done before lsm is freed below, because
1334          * cl_object still uses inode lsm.
1335          */
1336         cl_inode_fini(inode);
1337 }
1338
1339 #define TIMES_SET_FLAGS (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)
1340
1341 static int ll_md_setattr(struct dentry *dentry, struct md_op_data *op_data)
1342 {
1343         struct lustre_md md;
1344         struct inode *inode = d_inode(dentry);
1345         struct ll_sb_info *sbi = ll_i2sbi(inode);
1346         struct ptlrpc_request *request = NULL;
1347         int rc, ia_valid;
1348
1349         op_data = ll_prep_md_op_data(op_data, inode, NULL, NULL, 0, 0,
1350                                      LUSTRE_OPC_ANY, NULL);
1351         if (IS_ERR(op_data))
1352                 return PTR_ERR(op_data);
1353
1354         rc = md_setattr(sbi->ll_md_exp, op_data, NULL, 0, &request);
1355         if (rc) {
1356                 ptlrpc_req_finished(request);
1357                 if (rc == -ENOENT) {
1358                         clear_nlink(inode);
1359                         /* Unlinked special device node? Or just a race?
1360                          * Pretend we did everything.
1361                          */
1362                         if (!S_ISREG(inode->i_mode) &&
1363                             !S_ISDIR(inode->i_mode)) {
1364                                 ia_valid = op_data->op_attr.ia_valid;
1365                                 op_data->op_attr.ia_valid &= ~TIMES_SET_FLAGS;
1366                                 rc = simple_setattr(dentry, &op_data->op_attr);
1367                                 op_data->op_attr.ia_valid = ia_valid;
1368                         }
1369                 } else if (rc != -EPERM && rc != -EACCES && rc != -ETXTBSY) {
1370                         CERROR("md_setattr fails: rc = %d\n", rc);
1371                 }
1372                 return rc;
1373         }
1374
1375         rc = md_get_lustre_md(sbi->ll_md_exp, request, sbi->ll_dt_exp,
1376                               sbi->ll_md_exp, &md);
1377         if (rc) {
1378                 ptlrpc_req_finished(request);
1379                 return rc;
1380         }
1381
1382         ia_valid = op_data->op_attr.ia_valid;
1383         /* inode size will be in cl_setattr_ost, can't do it now since dirty
1384          * cache is not cleared yet.
1385          */
1386         op_data->op_attr.ia_valid &= ~(TIMES_SET_FLAGS | ATTR_SIZE);
1387         if (S_ISREG(inode->i_mode))
1388                 inode_lock(inode);
1389         rc = simple_setattr(dentry, &op_data->op_attr);
1390         if (S_ISREG(inode->i_mode))
1391                 inode_unlock(inode);
1392         op_data->op_attr.ia_valid = ia_valid;
1393
1394         rc = ll_update_inode(inode, &md);
1395         ptlrpc_req_finished(request);
1396
1397         return rc;
1398 }
1399
1400 /* If this inode has objects allocated to it (lsm != NULL), then the OST
1401  * object(s) determine the file size and mtime.  Otherwise, the MDS will
1402  * keep these values until such a time that objects are allocated for it.
1403  * We do the MDS operations first, as it is checking permissions for us.
1404  * We don't to the MDS RPC if there is nothing that we want to store there,
1405  * otherwise there is no harm in updating mtime/atime on the MDS if we are
1406  * going to do an RPC anyways.
1407  *
1408  * If we are doing a truncate, we will send the mtime and ctime updates
1409  * to the OST with the punch RPC, otherwise we do an explicit setattr RPC.
1410  * I don't believe it is possible to get e.g. ATTR_MTIME_SET and ATTR_SIZE
1411  * at the same time.
1412  *
1413  * In case of HSMimport, we only set attr on MDS.
1414  */
1415 int ll_setattr_raw(struct dentry *dentry, struct iattr *attr, bool hsm_import)
1416 {
1417         struct inode *inode = d_inode(dentry);
1418         struct ll_inode_info *lli = ll_i2info(inode);
1419         struct md_op_data *op_data = NULL;
1420         int rc = 0;
1421
1422         CDEBUG(D_VFSTRACE, "%s: setattr inode " DFID "(%p) from %llu to %llu, valid %x, hsm_import %d\n",
1423                ll_get_fsname(inode->i_sb, NULL, 0), PFID(&lli->lli_fid), inode,
1424                i_size_read(inode), attr->ia_size, attr->ia_valid, hsm_import);
1425
1426         if (attr->ia_valid & ATTR_SIZE) {
1427                 /* Check new size against VFS/VM file size limit and rlimit */
1428                 rc = inode_newsize_ok(inode, attr->ia_size);
1429                 if (rc)
1430                         return rc;
1431
1432                 /* The maximum Lustre file size is variable, based on the
1433                  * OST maximum object size and number of stripes.  This
1434                  * needs another check in addition to the VFS check above.
1435                  */
1436                 if (attr->ia_size > ll_file_maxbytes(inode)) {
1437                         CDEBUG(D_INODE, "file " DFID " too large %llu > %llu\n",
1438                                PFID(&lli->lli_fid), attr->ia_size,
1439                                ll_file_maxbytes(inode));
1440                         return -EFBIG;
1441                 }
1442
1443                 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
1444         }
1445
1446         /* POSIX: check before ATTR_*TIME_SET set (from setattr_prepare) */
1447         if (attr->ia_valid & TIMES_SET_FLAGS) {
1448                 if ((!uid_eq(current_fsuid(), inode->i_uid)) &&
1449                     !capable(CFS_CAP_FOWNER))
1450                         return -EPERM;
1451         }
1452
1453         /* We mark all of the fields "set" so MDS/OST does not re-set them */
1454         if (attr->ia_valid & ATTR_CTIME) {
1455                 attr->ia_ctime = current_time(inode);
1456                 attr->ia_valid |= ATTR_CTIME_SET;
1457         }
1458         if (!(attr->ia_valid & ATTR_ATIME_SET) &&
1459             (attr->ia_valid & ATTR_ATIME)) {
1460                 attr->ia_atime = current_time(inode);
1461                 attr->ia_valid |= ATTR_ATIME_SET;
1462         }
1463         if (!(attr->ia_valid & ATTR_MTIME_SET) &&
1464             (attr->ia_valid & ATTR_MTIME)) {
1465                 attr->ia_mtime = current_time(inode);
1466                 attr->ia_valid |= ATTR_MTIME_SET;
1467         }
1468
1469         if (attr->ia_valid & (ATTR_MTIME | ATTR_CTIME))
1470                 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu, now = %llu\n",
1471                        LTIME_S(attr->ia_mtime), LTIME_S(attr->ia_ctime),
1472                        (s64)ktime_get_real_seconds());
1473
1474         if (S_ISREG(inode->i_mode))
1475                 inode_unlock(inode);
1476
1477         /*
1478          * We always do an MDS RPC, even if we're only changing the size;
1479          * only the MDS knows whether truncate() should fail with -ETXTBUSY
1480          */
1481         op_data = kzalloc(sizeof(*op_data), GFP_NOFS);
1482         if (!op_data) {
1483                 rc = -ENOMEM;
1484                 goto out;
1485         }
1486
1487         if (!hsm_import && attr->ia_valid & ATTR_SIZE) {
1488                 /*
1489                  * If we are changing file size, file content is
1490                  * modified, flag it.
1491                  */
1492                 attr->ia_valid |= MDS_OPEN_OWNEROVERRIDE;
1493                 op_data->op_bias |= MDS_DATA_MODIFIED;
1494                 clear_bit(LLIF_DATA_MODIFIED, &lli->lli_flags);
1495         }
1496
1497         op_data->op_attr = *attr;
1498
1499         rc = ll_md_setattr(dentry, op_data);
1500         if (rc)
1501                 goto out;
1502
1503         if (!S_ISREG(inode->i_mode) || hsm_import) {
1504                 rc = 0;
1505                 goto out;
1506         }
1507
1508         if (attr->ia_valid & (ATTR_SIZE |
1509                               ATTR_ATIME | ATTR_ATIME_SET |
1510                               ATTR_MTIME | ATTR_MTIME_SET)) {
1511                 /* For truncate and utimes sending attributes to OSTs, setting
1512                  * mtime/atime to the past will be performed under PW [0:EOF]
1513                  * extent lock (new_size:EOF for truncate).  It may seem
1514                  * excessive to send mtime/atime updates to OSTs when not
1515                  * setting times to past, but it is necessary due to possible
1516                  * time de-synchronization between MDT inode and OST objects
1517                  */
1518                 rc = cl_setattr_ost(ll_i2info(inode)->lli_clob, attr, 0);
1519         }
1520
1521         /*
1522          * If the file was restored, it needs to set dirty flag.
1523          *
1524          * We've already sent MDS_DATA_MODIFIED flag in
1525          * ll_md_setattr() for truncate. However, the MDT refuses to
1526          * set the HS_DIRTY flag on released files, so we have to set
1527          * it again if the file has been restored. Please check how
1528          * LLIF_DATA_MODIFIED is set in vvp_io_setattr_fini().
1529          *
1530          * Please notice that if the file is not released, the previous
1531          * MDS_DATA_MODIFIED has taken effect and usually
1532          * LLIF_DATA_MODIFIED is not set(see vvp_io_setattr_fini()).
1533          * This way we can save an RPC for common open + trunc
1534          * operation.
1535          */
1536         if (test_and_clear_bit(LLIF_DATA_MODIFIED, &lli->lli_flags)) {
1537                 struct hsm_state_set hss = {
1538                         .hss_valid = HSS_SETMASK,
1539                         .hss_setmask = HS_DIRTY,
1540                 };
1541                 int rc2;
1542
1543                 rc2 = ll_hsm_state_set(inode, &hss);
1544                 /*
1545                  * truncate and write can happen at the same time, so that
1546                  * the file can be set modified even though the file is not
1547                  * restored from released state, and ll_hsm_state_set() is
1548                  * not applicable for the file, and rc2 < 0 is normal in this
1549                  * case.
1550                  */
1551                 if (rc2 < 0)
1552                         CDEBUG(D_INFO, DFID "HSM set dirty failed: rc2 = %d\n",
1553                                PFID(ll_inode2fid(inode)), rc2);
1554         }
1555
1556 out:
1557         if (op_data)
1558                 ll_finish_md_op_data(op_data);
1559
1560         if (S_ISREG(inode->i_mode)) {
1561                 inode_lock(inode);
1562                 if ((attr->ia_valid & ATTR_SIZE) && !hsm_import)
1563                         inode_dio_wait(inode);
1564         }
1565
1566         ll_stats_ops_tally(ll_i2sbi(inode), (attr->ia_valid & ATTR_SIZE) ?
1567                         LPROC_LL_TRUNC : LPROC_LL_SETATTR, 1);
1568
1569         return rc;
1570 }
1571
1572 int ll_setattr(struct dentry *de, struct iattr *attr)
1573 {
1574         int mode = d_inode(de)->i_mode;
1575
1576         if ((attr->ia_valid & (ATTR_CTIME | ATTR_SIZE | ATTR_MODE)) ==
1577                               (ATTR_CTIME | ATTR_SIZE | ATTR_MODE))
1578                 attr->ia_valid |= MDS_OPEN_OWNEROVERRIDE;
1579
1580         if (((attr->ia_valid & (ATTR_MODE | ATTR_FORCE | ATTR_SIZE)) ==
1581                                (ATTR_SIZE | ATTR_MODE)) &&
1582             (((mode & S_ISUID) && !(attr->ia_mode & S_ISUID)) ||
1583              (((mode & (S_ISGID | 0010)) == (S_ISGID | 0010)) &&
1584               !(attr->ia_mode & S_ISGID))))
1585                 attr->ia_valid |= ATTR_FORCE;
1586
1587         if ((attr->ia_valid & ATTR_MODE) &&
1588             (mode & S_ISUID) &&
1589             !(attr->ia_mode & S_ISUID) &&
1590             !(attr->ia_valid & ATTR_KILL_SUID))
1591                 attr->ia_valid |= ATTR_KILL_SUID;
1592
1593         if ((attr->ia_valid & ATTR_MODE) &&
1594             ((mode & (S_ISGID | 0010)) == (S_ISGID | 0010)) &&
1595             !(attr->ia_mode & S_ISGID) &&
1596             !(attr->ia_valid & ATTR_KILL_SGID))
1597                 attr->ia_valid |= ATTR_KILL_SGID;
1598
1599         return ll_setattr_raw(de, attr, false);
1600 }
1601
1602 int ll_statfs_internal(struct super_block *sb, struct obd_statfs *osfs,
1603                        __u64 max_age, __u32 flags)
1604 {
1605         struct ll_sb_info *sbi = ll_s2sbi(sb);
1606         struct obd_statfs obd_osfs;
1607         int rc;
1608
1609         rc = obd_statfs(NULL, sbi->ll_md_exp, osfs, max_age, flags);
1610         if (rc) {
1611                 CERROR("md_statfs fails: rc = %d\n", rc);
1612                 return rc;
1613         }
1614
1615         osfs->os_type = sb->s_magic;
1616
1617         CDEBUG(D_SUPER, "MDC blocks %llu/%llu objects %llu/%llu\n",
1618                osfs->os_bavail, osfs->os_blocks, osfs->os_ffree,
1619                osfs->os_files);
1620
1621         if (sbi->ll_flags & LL_SBI_LAZYSTATFS)
1622                 flags |= OBD_STATFS_NODELAY;
1623
1624         rc = obd_statfs_rqset(sbi->ll_dt_exp, &obd_osfs, max_age, flags);
1625         if (rc) {
1626                 CERROR("obd_statfs fails: rc = %d\n", rc);
1627                 return rc;
1628         }
1629
1630         CDEBUG(D_SUPER, "OSC blocks %llu/%llu objects %llu/%llu\n",
1631                obd_osfs.os_bavail, obd_osfs.os_blocks, obd_osfs.os_ffree,
1632                obd_osfs.os_files);
1633
1634         osfs->os_bsize = obd_osfs.os_bsize;
1635         osfs->os_blocks = obd_osfs.os_blocks;
1636         osfs->os_bfree = obd_osfs.os_bfree;
1637         osfs->os_bavail = obd_osfs.os_bavail;
1638
1639         /* If we don't have as many objects free on the OST as inodes
1640          * on the MDS, we reduce the total number of inodes to
1641          * compensate, so that the "inodes in use" number is correct.
1642          */
1643         if (obd_osfs.os_ffree < osfs->os_ffree) {
1644                 osfs->os_files = (osfs->os_files - osfs->os_ffree) +
1645                         obd_osfs.os_ffree;
1646                 osfs->os_ffree = obd_osfs.os_ffree;
1647         }
1648
1649         return rc;
1650 }
1651
1652 int ll_statfs(struct dentry *de, struct kstatfs *sfs)
1653 {
1654         struct super_block *sb = de->d_sb;
1655         struct obd_statfs osfs;
1656         int rc;
1657
1658         CDEBUG(D_VFSTRACE, "VFS Op: at %llu jiffies\n", get_jiffies_64());
1659         ll_stats_ops_tally(ll_s2sbi(sb), LPROC_LL_STAFS, 1);
1660
1661         /* Some amount of caching on the client is allowed */
1662         rc = ll_statfs_internal(sb, &osfs,
1663                                 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
1664                                 0);
1665         if (rc)
1666                 return rc;
1667
1668         statfs_unpack(sfs, &osfs);
1669
1670         /* We need to downshift for all 32-bit kernels, because we can't
1671          * tell if the kernel is being called via sys_statfs64() or not.
1672          * Stop before overflowing f_bsize - in which case it is better
1673          * to just risk EOVERFLOW if caller is using old sys_statfs().
1674          */
1675         if (sizeof(long) < 8) {
1676                 while (osfs.os_blocks > ~0UL && sfs->f_bsize < 0x40000000) {
1677                         sfs->f_bsize <<= 1;
1678
1679                         osfs.os_blocks >>= 1;
1680                         osfs.os_bfree >>= 1;
1681                         osfs.os_bavail >>= 1;
1682                 }
1683         }
1684
1685         sfs->f_blocks = osfs.os_blocks;
1686         sfs->f_bfree = osfs.os_bfree;
1687         sfs->f_bavail = osfs.os_bavail;
1688         sfs->f_fsid = ll_s2sbi(sb)->ll_fsid;
1689         return 0;
1690 }
1691
1692 void ll_inode_size_lock(struct inode *inode)
1693 {
1694         struct ll_inode_info *lli;
1695
1696         LASSERT(!S_ISDIR(inode->i_mode));
1697
1698         lli = ll_i2info(inode);
1699         mutex_lock(&lli->lli_size_mutex);
1700 }
1701
1702 void ll_inode_size_unlock(struct inode *inode)
1703 {
1704         struct ll_inode_info *lli;
1705
1706         lli = ll_i2info(inode);
1707         mutex_unlock(&lli->lli_size_mutex);
1708 }
1709
1710 int ll_update_inode(struct inode *inode, struct lustre_md *md)
1711 {
1712         struct ll_inode_info *lli = ll_i2info(inode);
1713         struct mdt_body *body = md->body;
1714         struct ll_sb_info *sbi = ll_i2sbi(inode);
1715
1716         if (body->mbo_valid & OBD_MD_FLEASIZE)
1717                 cl_file_inode_init(inode, md);
1718
1719         if (S_ISDIR(inode->i_mode)) {
1720                 int rc;
1721
1722                 rc = ll_update_lsm_md(inode, md);
1723                 if (rc)
1724                         return rc;
1725         }
1726
1727 #ifdef CONFIG_FS_POSIX_ACL
1728         if (body->mbo_valid & OBD_MD_FLACL) {
1729                 spin_lock(&lli->lli_lock);
1730                 if (lli->lli_posix_acl)
1731                         posix_acl_release(lli->lli_posix_acl);
1732                 lli->lli_posix_acl = md->posix_acl;
1733                 spin_unlock(&lli->lli_lock);
1734         }
1735 #endif
1736         inode->i_ino = cl_fid_build_ino(&body->mbo_fid1,
1737                                         sbi->ll_flags & LL_SBI_32BIT_API);
1738         inode->i_generation = cl_fid_build_gen(&body->mbo_fid1);
1739
1740         if (body->mbo_valid & OBD_MD_FLATIME) {
1741                 if (body->mbo_atime > LTIME_S(inode->i_atime))
1742                         LTIME_S(inode->i_atime) = body->mbo_atime;
1743                 lli->lli_atime = body->mbo_atime;
1744         }
1745         if (body->mbo_valid & OBD_MD_FLMTIME) {
1746                 if (body->mbo_mtime > LTIME_S(inode->i_mtime)) {
1747                         CDEBUG(D_INODE, "setting ino %lu mtime from %lu to %llu\n",
1748                                inode->i_ino, LTIME_S(inode->i_mtime),
1749                                body->mbo_mtime);
1750                         LTIME_S(inode->i_mtime) = body->mbo_mtime;
1751                 }
1752                 lli->lli_mtime = body->mbo_mtime;
1753         }
1754         if (body->mbo_valid & OBD_MD_FLCTIME) {
1755                 if (body->mbo_ctime > LTIME_S(inode->i_ctime))
1756                         LTIME_S(inode->i_ctime) = body->mbo_ctime;
1757                 lli->lli_ctime = body->mbo_ctime;
1758         }
1759         if (body->mbo_valid & OBD_MD_FLMODE)
1760                 inode->i_mode = (inode->i_mode & S_IFMT) |
1761                                 (body->mbo_mode & ~S_IFMT);
1762         if (body->mbo_valid & OBD_MD_FLTYPE)
1763                 inode->i_mode = (inode->i_mode & ~S_IFMT) |
1764                                 (body->mbo_mode & S_IFMT);
1765         LASSERT(inode->i_mode != 0);
1766         if (S_ISREG(inode->i_mode))
1767                 inode->i_blkbits = min(PTLRPC_MAX_BRW_BITS + 1,
1768                                        LL_MAX_BLKSIZE_BITS);
1769         else
1770                 inode->i_blkbits = inode->i_sb->s_blocksize_bits;
1771         if (body->mbo_valid & OBD_MD_FLUID)
1772                 inode->i_uid = make_kuid(&init_user_ns, body->mbo_uid);
1773         if (body->mbo_valid & OBD_MD_FLGID)
1774                 inode->i_gid = make_kgid(&init_user_ns, body->mbo_gid);
1775         if (body->mbo_valid & OBD_MD_FLFLAGS)
1776                 inode->i_flags = ll_ext_to_inode_flags(body->mbo_flags);
1777         if (body->mbo_valid & OBD_MD_FLNLINK)
1778                 set_nlink(inode, body->mbo_nlink);
1779         if (body->mbo_valid & OBD_MD_FLRDEV)
1780                 inode->i_rdev = old_decode_dev(body->mbo_rdev);
1781
1782         if (body->mbo_valid & OBD_MD_FLID) {
1783                 /* FID shouldn't be changed! */
1784                 if (fid_is_sane(&lli->lli_fid)) {
1785                         LASSERTF(lu_fid_eq(&lli->lli_fid, &body->mbo_fid1),
1786                                  "Trying to change FID " DFID " to the " DFID ", inode " DFID "(%p)\n",
1787                                  PFID(&lli->lli_fid), PFID(&body->mbo_fid1),
1788                                  PFID(ll_inode2fid(inode)), inode);
1789                 } else {
1790                         lli->lli_fid = body->mbo_fid1;
1791                 }
1792         }
1793
1794         LASSERT(fid_seq(&lli->lli_fid) != 0);
1795
1796         if (body->mbo_valid & OBD_MD_FLSIZE) {
1797                 i_size_write(inode, body->mbo_size);
1798
1799                 CDEBUG(D_VFSTRACE, "inode=" DFID ", updating i_size %llu\n",
1800                        PFID(ll_inode2fid(inode)),
1801                        (unsigned long long)body->mbo_size);
1802
1803                 if (body->mbo_valid & OBD_MD_FLBLOCKS)
1804                         inode->i_blocks = body->mbo_blocks;
1805         }
1806
1807         if (body->mbo_valid & OBD_MD_TSTATE) {
1808                 if (body->mbo_t_state & MS_RESTORE)
1809                         set_bit(LLIF_FILE_RESTORING, &lli->lli_flags);
1810         }
1811
1812         return 0;
1813 }
1814
1815 int ll_read_inode2(struct inode *inode, void *opaque)
1816 {
1817         struct lustre_md *md = opaque;
1818         struct ll_inode_info *lli = ll_i2info(inode);
1819         int rc;
1820
1821         CDEBUG(D_VFSTRACE, "VFS Op:inode=" DFID "(%p)\n",
1822                PFID(&lli->lli_fid), inode);
1823
1824         /* Core attributes from the MDS first.  This is a new inode, and
1825          * the VFS doesn't zero times in the core inode so we have to do
1826          * it ourselves.  They will be overwritten by either MDS or OST
1827          * attributes - we just need to make sure they aren't newer.
1828          */
1829         LTIME_S(inode->i_mtime) = 0;
1830         LTIME_S(inode->i_atime) = 0;
1831         LTIME_S(inode->i_ctime) = 0;
1832         inode->i_rdev = 0;
1833         rc = ll_update_inode(inode, md);
1834         if (rc)
1835                 return rc;
1836
1837         /* OIDEBUG(inode); */
1838
1839         if (S_ISREG(inode->i_mode)) {
1840                 struct ll_sb_info *sbi = ll_i2sbi(inode);
1841
1842                 inode->i_op = &ll_file_inode_operations;
1843                 inode->i_fop = sbi->ll_fop;
1844                 inode->i_mapping->a_ops = (struct address_space_operations *)&ll_aops;
1845         } else if (S_ISDIR(inode->i_mode)) {
1846                 inode->i_op = &ll_dir_inode_operations;
1847                 inode->i_fop = &ll_dir_operations;
1848         } else if (S_ISLNK(inode->i_mode)) {
1849                 inode->i_op = &ll_fast_symlink_inode_operations;
1850         } else {
1851                 inode->i_op = &ll_special_inode_operations;
1852
1853                 init_special_inode(inode, inode->i_mode,
1854                                    inode->i_rdev);
1855         }
1856
1857         return 0;
1858 }
1859
1860 void ll_delete_inode(struct inode *inode)
1861 {
1862         struct ll_inode_info *lli = ll_i2info(inode);
1863
1864         if (S_ISREG(inode->i_mode) && lli->lli_clob)
1865                 /* discard all dirty pages before truncating them, required by
1866                  * osc_extent implementation at LU-1030.
1867                  */
1868                 cl_sync_file_range(inode, 0, OBD_OBJECT_EOF,
1869                                    CL_FSYNC_LOCAL, 1);
1870
1871         truncate_inode_pages_final(&inode->i_data);
1872
1873         LASSERTF(!inode->i_data.nrpages,
1874                  "inode=" DFID "(%p) nrpages=%lu, see http://jira.whamcloud.com/browse/LU-118\n",
1875                  PFID(ll_inode2fid(inode)), inode, inode->i_data.nrpages);
1876
1877         ll_clear_inode(inode);
1878         clear_inode(inode);
1879 }
1880
1881 int ll_iocontrol(struct inode *inode, struct file *file,
1882                  unsigned int cmd, unsigned long arg)
1883 {
1884         struct ll_sb_info *sbi = ll_i2sbi(inode);
1885         struct ptlrpc_request *req = NULL;
1886         int rc, flags = 0;
1887
1888         switch (cmd) {
1889         case FSFILT_IOC_GETFLAGS: {
1890                 struct mdt_body *body;
1891                 struct md_op_data *op_data;
1892
1893                 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL,
1894                                              0, 0, LUSTRE_OPC_ANY,
1895                                              NULL);
1896                 if (IS_ERR(op_data))
1897                         return PTR_ERR(op_data);
1898
1899                 op_data->op_valid = OBD_MD_FLFLAGS;
1900                 rc = md_getattr(sbi->ll_md_exp, op_data, &req);
1901                 ll_finish_md_op_data(op_data);
1902                 if (rc) {
1903                         CERROR("%s: failure inode " DFID ": rc = %d\n",
1904                                sbi->ll_md_exp->exp_obd->obd_name,
1905                                PFID(ll_inode2fid(inode)), rc);
1906                         return -abs(rc);
1907                 }
1908
1909                 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
1910
1911                 flags = body->mbo_flags;
1912
1913                 ptlrpc_req_finished(req);
1914
1915                 return put_user(flags, (int __user *)arg);
1916         }
1917         case FSFILT_IOC_SETFLAGS: {
1918                 struct md_op_data *op_data;
1919                 struct cl_object *obj;
1920                 struct iattr *attr;
1921
1922                 if (get_user(flags, (int __user *)arg))
1923                         return -EFAULT;
1924
1925                 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
1926                                              LUSTRE_OPC_ANY, NULL);
1927                 if (IS_ERR(op_data))
1928                         return PTR_ERR(op_data);
1929
1930                 op_data->op_attr_flags = flags;
1931                 op_data->op_attr.ia_valid |= ATTR_ATTR_FLAG;
1932                 rc = md_setattr(sbi->ll_md_exp, op_data, NULL, 0, &req);
1933                 ll_finish_md_op_data(op_data);
1934                 ptlrpc_req_finished(req);
1935                 if (rc)
1936                         return rc;
1937
1938                 inode->i_flags = ll_ext_to_inode_flags(flags);
1939
1940                 obj = ll_i2info(inode)->lli_clob;
1941                 if (!obj)
1942                         return 0;
1943
1944                 attr = kzalloc(sizeof(*attr), GFP_NOFS);
1945                 if (!attr)
1946                         return -ENOMEM;
1947
1948                 attr->ia_valid = ATTR_ATTR_FLAG;
1949                 rc = cl_setattr_ost(obj, attr, flags);
1950                 kfree(attr);
1951                 return rc;
1952         }
1953         default:
1954                 return -ENOSYS;
1955         }
1956
1957         return 0;
1958 }
1959
1960 int ll_flush_ctx(struct inode *inode)
1961 {
1962         struct ll_sb_info  *sbi = ll_i2sbi(inode);
1963
1964         CDEBUG(D_SEC, "flush context for user %d\n",
1965                from_kuid(&init_user_ns, current_uid()));
1966
1967         obd_set_info_async(NULL, sbi->ll_md_exp,
1968                            sizeof(KEY_FLUSH_CTX), KEY_FLUSH_CTX,
1969                            0, NULL, NULL);
1970         obd_set_info_async(NULL, sbi->ll_dt_exp,
1971                            sizeof(KEY_FLUSH_CTX), KEY_FLUSH_CTX,
1972                            0, NULL, NULL);
1973         return 0;
1974 }
1975
1976 /* umount -f client means force down, don't save state */
1977 void ll_umount_begin(struct super_block *sb)
1978 {
1979         struct ll_sb_info *sbi = ll_s2sbi(sb);
1980         struct obd_device *obd;
1981         struct obd_ioctl_data *ioc_data;
1982         wait_queue_head_t waitq;
1983         struct l_wait_info lwi;
1984
1985         CDEBUG(D_VFSTRACE, "VFS Op: superblock %p count %d active %d\n", sb,
1986                sb->s_count, atomic_read(&sb->s_active));
1987
1988         obd = class_exp2obd(sbi->ll_md_exp);
1989         if (!obd) {
1990                 CERROR("Invalid MDC connection handle %#llx\n",
1991                        sbi->ll_md_exp->exp_handle.h_cookie);
1992                 return;
1993         }
1994         obd->obd_force = 1;
1995
1996         obd = class_exp2obd(sbi->ll_dt_exp);
1997         if (!obd) {
1998                 CERROR("Invalid LOV connection handle %#llx\n",
1999                        sbi->ll_dt_exp->exp_handle.h_cookie);
2000                 return;
2001         }
2002         obd->obd_force = 1;
2003
2004         ioc_data = kzalloc(sizeof(*ioc_data), GFP_NOFS);
2005         if (ioc_data) {
2006                 obd_iocontrol(IOC_OSC_SET_ACTIVE, sbi->ll_md_exp,
2007                               sizeof(*ioc_data), ioc_data, NULL);
2008
2009                 obd_iocontrol(IOC_OSC_SET_ACTIVE, sbi->ll_dt_exp,
2010                               sizeof(*ioc_data), ioc_data, NULL);
2011
2012                 kfree(ioc_data);
2013         }
2014
2015         /* Really, we'd like to wait until there are no requests outstanding,
2016          * and then continue. For now, we just periodically checking for vfs
2017          * to decrement mnt_cnt and hope to finish it within 10sec.
2018          */
2019         init_waitqueue_head(&waitq);
2020         lwi = LWI_TIMEOUT_INTERVAL(cfs_time_seconds(10),
2021                                    cfs_time_seconds(1), NULL, NULL);
2022         l_wait_event(waitq, may_umount(sbi->ll_mnt.mnt), &lwi);
2023
2024         schedule();
2025 }
2026
2027 int ll_remount_fs(struct super_block *sb, int *flags, char *data)
2028 {
2029         struct ll_sb_info *sbi = ll_s2sbi(sb);
2030         char *profilenm = get_profile_name(sb);
2031         int err;
2032         __u32 read_only;
2033
2034         if ((bool)(*flags & MS_RDONLY) != sb_rdonly(sb)) {
2035                 read_only = *flags & MS_RDONLY;
2036                 err = obd_set_info_async(NULL, sbi->ll_md_exp,
2037                                          sizeof(KEY_READ_ONLY),
2038                                          KEY_READ_ONLY, sizeof(read_only),
2039                                          &read_only, NULL);
2040                 if (err) {
2041                         LCONSOLE_WARN("Failed to remount %s %s (%d)\n",
2042                                       profilenm, read_only ?
2043                                       "read-only" : "read-write", err);
2044                         return err;
2045                 }
2046
2047                 if (read_only)
2048                         sb->s_flags |= MS_RDONLY;
2049                 else
2050                         sb->s_flags &= ~MS_RDONLY;
2051
2052                 if (sbi->ll_flags & LL_SBI_VERBOSE)
2053                         LCONSOLE_WARN("Remounted %s %s\n", profilenm,
2054                                       read_only ?  "read-only" : "read-write");
2055         }
2056         return 0;
2057 }
2058
2059 /**
2060  * Cleanup the open handle that is cached on MDT-side.
2061  *
2062  * For open case, the client side open handling thread may hit error
2063  * after the MDT grant the open. Under such case, the client should
2064  * send close RPC to the MDT as cleanup; otherwise, the open handle
2065  * on the MDT will be leaked there until the client umount or evicted.
2066  *
2067  * In further, if someone unlinked the file, because the open handle
2068  * holds the reference on such file/object, then it will block the
2069  * subsequent threads that want to locate such object via FID.
2070  *
2071  * \param[in] sb        super block for this file-system
2072  * \param[in] open_req  pointer to the original open request
2073  */
2074 void ll_open_cleanup(struct super_block *sb, struct ptlrpc_request *open_req)
2075 {
2076         struct mdt_body                 *body;
2077         struct md_op_data               *op_data;
2078         struct ptlrpc_request           *close_req = NULL;
2079         struct obd_export               *exp       = ll_s2sbi(sb)->ll_md_exp;
2080
2081         body = req_capsule_server_get(&open_req->rq_pill, &RMF_MDT_BODY);
2082         op_data = kzalloc(sizeof(*op_data), GFP_NOFS);
2083         if (!op_data)
2084                 return;
2085
2086         op_data->op_fid1 = body->mbo_fid1;
2087         op_data->op_handle = body->mbo_handle;
2088         op_data->op_mod_time = get_seconds();
2089         md_close(exp, op_data, NULL, &close_req);
2090         ptlrpc_req_finished(close_req);
2091         ll_finish_md_op_data(op_data);
2092 }
2093
2094 int ll_prep_inode(struct inode **inode, struct ptlrpc_request *req,
2095                   struct super_block *sb, struct lookup_intent *it)
2096 {
2097         struct ll_sb_info *sbi = NULL;
2098         struct lustre_md md = { NULL };
2099         int rc;
2100
2101         LASSERT(*inode || sb);
2102         sbi = sb ? ll_s2sbi(sb) : ll_i2sbi(*inode);
2103         rc = md_get_lustre_md(sbi->ll_md_exp, req, sbi->ll_dt_exp,
2104                               sbi->ll_md_exp, &md);
2105         if (rc)
2106                 goto cleanup;
2107
2108         if (*inode) {
2109                 rc = ll_update_inode(*inode, &md);
2110                 if (rc)
2111                         goto out;
2112         } else {
2113                 LASSERT(sb);
2114
2115                 /*
2116                  * At this point server returns to client's same fid as client
2117                  * generated for creating. So using ->fid1 is okay here.
2118                  */
2119                 if (!fid_is_sane(&md.body->mbo_fid1)) {
2120                         CERROR("%s: Fid is insane " DFID "\n",
2121                                ll_get_fsname(sb, NULL, 0),
2122                                PFID(&md.body->mbo_fid1));
2123                         rc = -EINVAL;
2124                         goto out;
2125                 }
2126
2127                 *inode = ll_iget(sb, cl_fid_build_ino(&md.body->mbo_fid1,
2128                                              sbi->ll_flags & LL_SBI_32BIT_API),
2129                                  &md);
2130                 if (IS_ERR(*inode)) {
2131 #ifdef CONFIG_FS_POSIX_ACL
2132                         if (md.posix_acl) {
2133                                 posix_acl_release(md.posix_acl);
2134                                 md.posix_acl = NULL;
2135                         }
2136 #endif
2137                         rc = -ENOMEM;
2138                         CERROR("new_inode -fatal: rc %d\n", rc);
2139                         goto out;
2140                 }
2141         }
2142
2143         /* Handling piggyback layout lock.
2144          * Layout lock can be piggybacked by getattr and open request.
2145          * The lsm can be applied to inode only if it comes with a layout lock
2146          * otherwise correct layout may be overwritten, for example:
2147          * 1. proc1: mdt returns a lsm but not granting layout
2148          * 2. layout was changed by another client
2149          * 3. proc2: refresh layout and layout lock granted
2150          * 4. proc1: to apply a stale layout
2151          */
2152         if (it && it->it_lock_mode != 0) {
2153                 struct lustre_handle lockh;
2154                 struct ldlm_lock *lock;
2155
2156                 lockh.cookie = it->it_lock_handle;
2157                 lock = ldlm_handle2lock(&lockh);
2158                 LASSERT(lock);
2159                 if (ldlm_has_layout(lock)) {
2160                         struct cl_object_conf conf;
2161
2162                         memset(&conf, 0, sizeof(conf));
2163                         conf.coc_opc = OBJECT_CONF_SET;
2164                         conf.coc_inode = *inode;
2165                         conf.coc_lock = lock;
2166                         conf.u.coc_layout = md.layout;
2167                         (void)ll_layout_conf(*inode, &conf);
2168                 }
2169                 LDLM_LOCK_PUT(lock);
2170         }
2171
2172 out:
2173         md_free_lustre_md(sbi->ll_md_exp, &md);
2174 cleanup:
2175         if (rc != 0 && it && it->it_op & IT_OPEN)
2176                 ll_open_cleanup(sb ? sb : (*inode)->i_sb, req);
2177
2178         return rc;
2179 }
2180
2181 int ll_obd_statfs(struct inode *inode, void __user *arg)
2182 {
2183         struct ll_sb_info *sbi = NULL;
2184         struct obd_export *exp;
2185         char *buf = NULL;
2186         struct obd_ioctl_data *data = NULL;
2187         __u32 type;
2188         int len = 0, rc;
2189
2190         if (!inode) {
2191                 rc = -EINVAL;
2192                 goto out_statfs;
2193         }
2194
2195         sbi = ll_i2sbi(inode);
2196         if (!sbi) {
2197                 rc = -EINVAL;
2198                 goto out_statfs;
2199         }
2200
2201         rc = obd_ioctl_getdata(&buf, &len, arg);
2202         if (rc)
2203                 goto out_statfs;
2204
2205         data = (void *)buf;
2206         if (!data->ioc_inlbuf1 || !data->ioc_inlbuf2 ||
2207             !data->ioc_pbuf1 || !data->ioc_pbuf2) {
2208                 rc = -EINVAL;
2209                 goto out_statfs;
2210         }
2211
2212         if (data->ioc_inllen1 != sizeof(__u32) ||
2213             data->ioc_inllen2 != sizeof(__u32) ||
2214             data->ioc_plen1 != sizeof(struct obd_statfs) ||
2215             data->ioc_plen2 != sizeof(struct obd_uuid)) {
2216                 rc = -EINVAL;
2217                 goto out_statfs;
2218         }
2219
2220         memcpy(&type, data->ioc_inlbuf1, sizeof(__u32));
2221         if (type & LL_STATFS_LMV) {
2222                 exp = sbi->ll_md_exp;
2223         } else if (type & LL_STATFS_LOV) {
2224                 exp = sbi->ll_dt_exp;
2225         } else {
2226                 rc = -ENODEV;
2227                 goto out_statfs;
2228         }
2229
2230         rc = obd_iocontrol(IOC_OBD_STATFS, exp, len, buf, NULL);
2231         if (rc)
2232                 goto out_statfs;
2233 out_statfs:
2234         kvfree(buf);
2235         return rc;
2236 }
2237
2238 int ll_process_config(struct lustre_cfg *lcfg)
2239 {
2240         char *ptr;
2241         void *sb;
2242         struct lprocfs_static_vars lvars;
2243         unsigned long x;
2244         int rc = 0;
2245
2246         lprocfs_llite_init_vars(&lvars);
2247
2248         /* The instance name contains the sb: lustre-client-aacfe000 */
2249         ptr = strrchr(lustre_cfg_string(lcfg, 0), '-');
2250         if (!ptr || !*(++ptr))
2251                 return -EINVAL;
2252         rc = kstrtoul(ptr, 16, &x);
2253         if (rc != 0)
2254                 return -EINVAL;
2255         sb = (void *)x;
2256         /* This better be a real Lustre superblock! */
2257         LASSERT(s2lsi((struct super_block *)sb)->lsi_lmd->lmd_magic == LMD_MAGIC);
2258
2259         /* Note we have not called client_common_fill_super yet, so
2260          * proc fns must be able to handle that!
2261          */
2262         rc = class_process_proc_param(PARAM_LLITE, lvars.obd_vars,
2263                                       lcfg, sb);
2264         if (rc > 0)
2265                 rc = 0;
2266         return rc;
2267 }
2268
2269 /* this function prepares md_op_data hint for passing ot down to MD stack. */
2270 struct md_op_data *ll_prep_md_op_data(struct md_op_data *op_data,
2271                                       struct inode *i1, struct inode *i2,
2272                                       const char *name, size_t namelen,
2273                                       u32 mode, __u32 opc, void *data)
2274 {
2275         if (!name) {
2276                 /* Do not reuse namelen for something else. */
2277                 if (namelen)
2278                         return ERR_PTR(-EINVAL);
2279         } else {
2280                 if (namelen > ll_i2sbi(i1)->ll_namelen)
2281                         return ERR_PTR(-ENAMETOOLONG);
2282
2283                 if (!lu_name_is_valid_2(name, namelen))
2284                         return ERR_PTR(-EINVAL);
2285         }
2286
2287         if (!op_data)
2288                 op_data = kzalloc(sizeof(*op_data), GFP_NOFS);
2289
2290         if (!op_data)
2291                 return ERR_PTR(-ENOMEM);
2292
2293         ll_i2gids(op_data->op_suppgids, i1, i2);
2294         op_data->op_fid1 = *ll_inode2fid(i1);
2295         op_data->op_default_stripe_offset = -1;
2296         if (S_ISDIR(i1->i_mode)) {
2297                 op_data->op_mea1 = ll_i2info(i1)->lli_lsm_md;
2298                 if (opc == LUSTRE_OPC_MKDIR)
2299                         op_data->op_default_stripe_offset =
2300                                 ll_i2info(i1)->lli_def_stripe_offset;
2301         }
2302
2303         if (i2) {
2304                 op_data->op_fid2 = *ll_inode2fid(i2);
2305                 if (S_ISDIR(i2->i_mode))
2306                         op_data->op_mea2 = ll_i2info(i2)->lli_lsm_md;
2307         } else {
2308                 fid_zero(&op_data->op_fid2);
2309         }
2310
2311         if (ll_i2sbi(i1)->ll_flags & LL_SBI_64BIT_HASH)
2312                 op_data->op_cli_flags |= CLI_HASH64;
2313
2314         if (ll_need_32bit_api(ll_i2sbi(i1)))
2315                 op_data->op_cli_flags |= CLI_API32;
2316
2317         op_data->op_name = name;
2318         op_data->op_namelen = namelen;
2319         op_data->op_mode = mode;
2320         op_data->op_mod_time = ktime_get_real_seconds();
2321         op_data->op_fsuid = from_kuid(&init_user_ns, current_fsuid());
2322         op_data->op_fsgid = from_kgid(&init_user_ns, current_fsgid());
2323         op_data->op_cap = cfs_curproc_cap_pack();
2324         if ((opc == LUSTRE_OPC_CREATE) && name &&
2325             filename_is_volatile(name, namelen, &op_data->op_mds))
2326                 op_data->op_bias |= MDS_CREATE_VOLATILE;
2327         else
2328                 op_data->op_mds = 0;
2329         op_data->op_data = data;
2330
2331         return op_data;
2332 }
2333
2334 void ll_finish_md_op_data(struct md_op_data *op_data)
2335 {
2336         kfree(op_data);
2337 }
2338
2339 int ll_show_options(struct seq_file *seq, struct dentry *dentry)
2340 {
2341         struct ll_sb_info *sbi;
2342
2343         LASSERT(seq && dentry);
2344         sbi = ll_s2sbi(dentry->d_sb);
2345
2346         if (sbi->ll_flags & LL_SBI_NOLCK)
2347                 seq_puts(seq, ",nolock");
2348
2349         if (sbi->ll_flags & LL_SBI_FLOCK)
2350                 seq_puts(seq, ",flock");
2351
2352         if (sbi->ll_flags & LL_SBI_LOCALFLOCK)
2353                 seq_puts(seq, ",localflock");
2354
2355         if (sbi->ll_flags & LL_SBI_USER_XATTR)
2356                 seq_puts(seq, ",user_xattr");
2357
2358         if (sbi->ll_flags & LL_SBI_LAZYSTATFS)
2359                 seq_puts(seq, ",lazystatfs");
2360
2361         if (sbi->ll_flags & LL_SBI_USER_FID2PATH)
2362                 seq_puts(seq, ",user_fid2path");
2363
2364         if (sbi->ll_flags & LL_SBI_ALWAYS_PING)
2365                 seq_puts(seq, ",always_ping");
2366
2367         return 0;
2368 }
2369
2370 /**
2371  * Get obd name by cmd, and copy out to user space
2372  */
2373 int ll_get_obd_name(struct inode *inode, unsigned int cmd, unsigned long arg)
2374 {
2375         struct ll_sb_info *sbi = ll_i2sbi(inode);
2376         struct obd_device *obd;
2377
2378         if (cmd == OBD_IOC_GETDTNAME)
2379                 obd = class_exp2obd(sbi->ll_dt_exp);
2380         else if (cmd == OBD_IOC_GETMDNAME)
2381                 obd = class_exp2obd(sbi->ll_md_exp);
2382         else
2383                 return -EINVAL;
2384
2385         if (!obd)
2386                 return -ENOENT;
2387
2388         if (copy_to_user((void __user *)arg, obd->obd_name,
2389                          strlen(obd->obd_name) + 1))
2390                 return -EFAULT;
2391
2392         return 0;
2393 }
2394
2395 /**
2396  * Get lustre file system name by \a sbi. If \a buf is provided(non-NULL), the
2397  * fsname will be returned in this buffer; otherwise, a static buffer will be
2398  * used to store the fsname and returned to caller.
2399  */
2400 char *ll_get_fsname(struct super_block *sb, char *buf, int buflen)
2401 {
2402         static char fsname_static[MTI_NAME_MAXLEN];
2403         struct lustre_sb_info *lsi = s2lsi(sb);
2404         char *ptr;
2405         int len;
2406
2407         if (!buf) {
2408                 /* this means the caller wants to use static buffer
2409                  * and it doesn't care about race. Usually this is
2410                  * in error reporting path
2411                  */
2412                 buf = fsname_static;
2413                 buflen = sizeof(fsname_static);
2414         }
2415
2416         len = strlen(lsi->lsi_lmd->lmd_profile);
2417         ptr = strrchr(lsi->lsi_lmd->lmd_profile, '-');
2418         if (ptr && (strcmp(ptr, "-client") == 0))
2419                 len -= 7;
2420
2421         if (unlikely(len >= buflen))
2422                 len = buflen - 1;
2423         strncpy(buf, lsi->lsi_lmd->lmd_profile, len);
2424         buf[len] = '\0';
2425
2426         return buf;
2427 }
2428
2429 void ll_dirty_page_discard_warn(struct page *page, int ioret)
2430 {
2431         char *buf, *path = NULL;
2432         struct dentry *dentry = NULL;
2433         struct vvp_object *obj = cl_inode2vvp(page->mapping->host);
2434
2435         /* this can be called inside spin lock so use GFP_ATOMIC. */
2436         buf = (char *)__get_free_page(GFP_ATOMIC);
2437         if (buf) {
2438                 dentry = d_find_alias(page->mapping->host);
2439                 if (dentry)
2440                         path = dentry_path_raw(dentry, buf, PAGE_SIZE);
2441         }
2442
2443         CDEBUG(D_WARNING,
2444                "%s: dirty page discard: %s/fid: " DFID "/%s may get corrupted (rc %d)\n",
2445                ll_get_fsname(page->mapping->host->i_sb, NULL, 0),
2446                s2lsi(page->mapping->host->i_sb)->lsi_lmd->lmd_dev,
2447                PFID(&obj->vob_header.coh_lu.loh_fid),
2448                (path && !IS_ERR(path)) ? path : "", ioret);
2449
2450         if (dentry)
2451                 dput(dentry);
2452
2453         if (buf)
2454                 free_page((unsigned long)buf);
2455 }
2456
2457 ssize_t ll_copy_user_md(const struct lov_user_md __user *md,
2458                         struct lov_user_md **kbuf)
2459 {
2460         struct lov_user_md lum;
2461         ssize_t lum_size;
2462
2463         if (copy_from_user(&lum, md, sizeof(lum))) {
2464                 lum_size = -EFAULT;
2465                 goto no_kbuf;
2466         }
2467
2468         lum_size = ll_lov_user_md_size(&lum);
2469         if (lum_size < 0)
2470                 goto no_kbuf;
2471
2472         *kbuf = kzalloc(lum_size, GFP_NOFS);
2473         if (!*kbuf) {
2474                 lum_size = -ENOMEM;
2475                 goto no_kbuf;
2476         }
2477
2478         if (copy_from_user(*kbuf, md, lum_size) != 0) {
2479                 kfree(*kbuf);
2480                 *kbuf = NULL;
2481                 lum_size = -EFAULT;
2482         }
2483 no_kbuf:
2484         return lum_size;
2485 }
2486
2487 /*
2488  * Compute llite root squash state after a change of root squash
2489  * configuration setting or add/remove of a lnet nid
2490  */
2491 void ll_compute_rootsquash_state(struct ll_sb_info *sbi)
2492 {
2493         struct root_squash_info *squash = &sbi->ll_squash;
2494         struct lnet_process_id id;
2495         bool matched;
2496         int i;
2497
2498         /* Update norootsquash flag */
2499         down_write(&squash->rsi_sem);
2500         if (list_empty(&squash->rsi_nosquash_nids)) {
2501                 sbi->ll_flags &= ~LL_SBI_NOROOTSQUASH;
2502         } else {
2503                 /*
2504                  * Do not apply root squash as soon as one of our NIDs is
2505                  * in the nosquash_nids list
2506                  */
2507                 matched = false;
2508                 i = 0;
2509
2510                 while (LNetGetId(i++, &id) != -ENOENT) {
2511                         if (LNET_NETTYP(LNET_NIDNET(id.nid)) == LOLND)
2512                                 continue;
2513                         if (cfs_match_nid(id.nid, &squash->rsi_nosquash_nids)) {
2514                                 matched = true;
2515                                 break;
2516                         }
2517                 }
2518                 if (matched)
2519                         sbi->ll_flags |= LL_SBI_NOROOTSQUASH;
2520                 else
2521                         sbi->ll_flags &= ~LL_SBI_NOROOTSQUASH;
2522         }
2523         up_write(&squash->rsi_sem);
2524 }
2525
2526 /**
2527  * Parse linkea content to extract information about a given hardlink
2528  *
2529  * \param[in]   ldata           - Initialized linkea data
2530  * \param[in]   linkno          - Link identifier
2531  * \param[out]  parent_fid      - The entry's parent FID
2532  * \param[in]   size            - Entry name destination buffer
2533  *
2534  * \retval 0 on success
2535  * \retval Appropriate negative error code on failure
2536  */
2537 static int ll_linkea_decode(struct linkea_data *ldata, unsigned int linkno,
2538                             struct lu_fid *parent_fid, struct lu_name *ln)
2539 {
2540         unsigned int idx;
2541         int rc;
2542
2543         rc = linkea_init_with_rec(ldata);
2544         if (rc < 0)
2545                 return rc;
2546
2547         if (linkno >= ldata->ld_leh->leh_reccount)
2548                 /* beyond last link */
2549                 return -ENODATA;
2550
2551         linkea_first_entry(ldata);
2552         for (idx = 0; ldata->ld_lee; idx++) {
2553                 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen, ln,
2554                                     parent_fid);
2555                 if (idx == linkno)
2556                         break;
2557
2558                 linkea_next_entry(ldata);
2559         }
2560
2561         if (idx < linkno)
2562                 return -ENODATA;
2563
2564         return 0;
2565 }
2566
2567 /**
2568  * Get parent FID and name of an identified link. Operation is performed for
2569  * a given link number, letting the caller iterate over linkno to list one or
2570  * all links of an entry.
2571  *
2572  * \param[in]     file  - File descriptor against which to perform the operation
2573  * \param[in,out] arg   - User-filled structure containing the linkno to operate
2574  *                        on and the available size. It is eventually filled with
2575  *                        the requested information or left untouched on error
2576  *
2577  * \retval - 0 on success
2578  * \retval - Appropriate negative error code on failure
2579  */
2580 int ll_getparent(struct file *file, struct getparent __user *arg)
2581 {
2582         struct inode *inode = file_inode(file);
2583         struct linkea_data *ldata;
2584         struct lu_fid parent_fid;
2585         struct lu_buf buf = {
2586                 .lb_buf = NULL,
2587                 .lb_len = 0
2588         };
2589         struct lu_name ln;
2590         u32 name_size;
2591         u32 linkno;
2592         int rc;
2593
2594         if (!capable(CFS_CAP_DAC_READ_SEARCH) &&
2595             !(ll_i2sbi(inode)->ll_flags & LL_SBI_USER_FID2PATH))
2596                 return -EPERM;
2597
2598         if (get_user(name_size, &arg->gp_name_size))
2599                 return -EFAULT;
2600
2601         if (get_user(linkno, &arg->gp_linkno))
2602                 return -EFAULT;
2603
2604         if (name_size > PATH_MAX)
2605                 return -EINVAL;
2606
2607         ldata = kzalloc(sizeof(*ldata), GFP_NOFS);
2608         if (!ldata)
2609                 return -ENOMEM;
2610
2611         rc = linkea_data_new(ldata, &buf);
2612         if (rc < 0)
2613                 goto ldata_free;
2614
2615         rc = ll_xattr_list(inode, XATTR_NAME_LINK, XATTR_TRUSTED_T, buf.lb_buf,
2616                            buf.lb_len, OBD_MD_FLXATTR);
2617         if (rc < 0)
2618                 goto lb_free;
2619
2620         rc = ll_linkea_decode(ldata, linkno, &parent_fid, &ln);
2621         if (rc < 0)
2622                 goto lb_free;
2623
2624         if (ln.ln_namelen >= name_size) {
2625                 rc = -EOVERFLOW;
2626                 goto lb_free;
2627         }
2628
2629         if (copy_to_user(&arg->gp_fid, &parent_fid, sizeof(arg->gp_fid))) {
2630                 rc = -EFAULT;
2631                 goto lb_free;
2632         }
2633
2634         if (copy_to_user(&arg->gp_name, ln.ln_name, ln.ln_namelen)) {
2635                 rc = -EFAULT;
2636                 goto lb_free;
2637         }
2638
2639         if (put_user('\0', arg->gp_name + ln.ln_namelen)) {
2640                 rc = -EFAULT;
2641                 goto lb_free;
2642         }
2643
2644 lb_free:
2645         lu_buf_free(&buf);
2646 ldata_free:
2647         kfree(ldata);
2648         return rc;
2649 }