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