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