lustre: Convert to separately allocated bdi
[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 int ll_fill_super(struct super_block *sb, struct vfsmount *mnt)
867 {
868         struct lustre_profile *lprof = NULL;
869         struct lustre_sb_info *lsi = s2lsi(sb);
870         struct ll_sb_info *sbi;
871         char  *dt = NULL, *md = NULL;
872         char  *profilenm = get_profile_name(sb);
873         struct config_llog_instance *cfg;
874         int    err;
875         static atomic_t ll_bdi_num = ATOMIC_INIT(0);
876
877         CDEBUG(D_VFSTRACE, "VFS Op: sb %p\n", sb);
878
879         cfg = kzalloc(sizeof(*cfg), GFP_NOFS);
880         if (!cfg)
881                 return -ENOMEM;
882
883         try_module_get(THIS_MODULE);
884
885         /* client additional sb info */
886         sbi = ll_init_sbi(sb);
887         lsi->lsi_llsbi = sbi;
888         if (!sbi) {
889                 module_put(THIS_MODULE);
890                 kfree(cfg);
891                 return -ENOMEM;
892         }
893
894         err = ll_options(lsi->lsi_lmd->lmd_opts, &sbi->ll_flags);
895         if (err)
896                 goto out_free;
897
898         err = super_setup_bdi_name(sb, "lustre-%d",
899                                    atomic_inc_return(&ll_bdi_num));
900         if (err)
901                 goto out_free;
902
903         /* kernel >= 2.6.38 store dentry operations in sb->s_d_op. */
904         sb->s_d_op = &ll_d_ops;
905
906         /* Generate a string unique to this super, in case some joker tries
907          * to mount the same fs at two mount points.
908          * Use the address of the super itself.
909          */
910         cfg->cfg_instance = sb;
911         cfg->cfg_uuid = lsi->lsi_llsbi->ll_sb_uuid;
912         cfg->cfg_callback = class_config_llog_handler;
913         /* set up client obds */
914         err = lustre_process_log(sb, profilenm, cfg);
915         if (err < 0)
916                 goto out_free;
917
918         /* Profile set with LCFG_MOUNTOPT so we can find our mdc and osc obds */
919         lprof = class_get_profile(profilenm);
920         if (!lprof) {
921                 LCONSOLE_ERROR_MSG(0x156, "The client profile '%s' could not be read from the MGS.  Does that filesystem exist?\n",
922                                    profilenm);
923                 err = -EINVAL;
924                 goto out_free;
925         }
926         CDEBUG(D_CONFIG, "Found profile %s: mdc=%s osc=%s\n", profilenm,
927                lprof->lp_md, lprof->lp_dt);
928
929         dt = kasprintf(GFP_NOFS, "%s-%p", lprof->lp_dt, cfg->cfg_instance);
930         if (!dt) {
931                 err = -ENOMEM;
932                 goto out_free;
933         }
934
935         md = kasprintf(GFP_NOFS, "%s-%p", lprof->lp_md, cfg->cfg_instance);
936         if (!md) {
937                 err = -ENOMEM;
938                 goto out_free;
939         }
940
941         /* connections, registrations, sb setup */
942         err = client_common_fill_super(sb, md, dt, mnt);
943         if (!err)
944                 sbi->ll_client_common_fill_super_succeeded = 1;
945
946 out_free:
947         kfree(md);
948         kfree(dt);
949         if (lprof)
950                 class_put_profile(lprof);
951         if (err)
952                 ll_put_super(sb);
953         else if (sbi->ll_flags & LL_SBI_VERBOSE)
954                 LCONSOLE_WARN("Mounted %s\n", profilenm);
955
956         kfree(cfg);
957         return err;
958 } /* ll_fill_super */
959
960 void ll_put_super(struct super_block *sb)
961 {
962         struct config_llog_instance cfg, params_cfg;
963         struct obd_device *obd;
964         struct lustre_sb_info *lsi = s2lsi(sb);
965         struct ll_sb_info *sbi = ll_s2sbi(sb);
966         char *profilenm = get_profile_name(sb);
967         int next, force = 1, rc = 0;
968         long ccc_count;
969
970         CDEBUG(D_VFSTRACE, "VFS Op: sb %p - %s\n", sb, profilenm);
971
972         cfg.cfg_instance = sb;
973         lustre_end_log(sb, profilenm, &cfg);
974
975         params_cfg.cfg_instance = sb;
976         lustre_end_log(sb, PARAMS_FILENAME, &params_cfg);
977
978         if (sbi->ll_md_exp) {
979                 obd = class_exp2obd(sbi->ll_md_exp);
980                 if (obd)
981                         force = obd->obd_force;
982         }
983
984         /* Wait for unstable pages to be committed to stable storage */
985         if (!force) {
986                 struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
987
988                 rc = l_wait_event(sbi->ll_cache->ccc_unstable_waitq,
989                                   !atomic_long_read(&sbi->ll_cache->ccc_unstable_nr),
990                                   &lwi);
991         }
992
993         ccc_count = atomic_long_read(&sbi->ll_cache->ccc_unstable_nr);
994         if (!force && rc != -EINTR)
995                 LASSERTF(!ccc_count, "count: %li\n", ccc_count);
996
997         /* We need to set force before the lov_disconnect in
998          * lustre_common_put_super, since l_d cleans up osc's as well.
999          */
1000         if (force) {
1001                 next = 0;
1002                 while ((obd = class_devices_in_group(&sbi->ll_sb_uuid,
1003                                                      &next)) != NULL) {
1004                         obd->obd_force = force;
1005                 }
1006         }
1007
1008         if (sbi->ll_client_common_fill_super_succeeded) {
1009                 /* Only if client_common_fill_super succeeded */
1010                 client_common_put_super(sb);
1011         }
1012
1013         next = 0;
1014         while ((obd = class_devices_in_group(&sbi->ll_sb_uuid, &next)))
1015                 class_manual_cleanup(obd);
1016
1017         if (sbi->ll_flags & LL_SBI_VERBOSE)
1018                 LCONSOLE_WARN("Unmounted %s\n", profilenm ? profilenm : "");
1019
1020         if (profilenm)
1021                 class_del_profile(profilenm);
1022
1023         ll_free_sbi(sb);
1024         lsi->lsi_llsbi = NULL;
1025
1026         lustre_common_put_super(sb);
1027
1028         cl_env_cache_purge(~0);
1029
1030         module_put(THIS_MODULE);
1031 } /* client_put_super */
1032
1033 struct inode *ll_inode_from_resource_lock(struct ldlm_lock *lock)
1034 {
1035         struct inode *inode = NULL;
1036
1037         /* NOTE: we depend on atomic igrab() -bzzz */
1038         lock_res_and_lock(lock);
1039         if (lock->l_resource->lr_lvb_inode) {
1040                 struct ll_inode_info *lli;
1041
1042                 lli = ll_i2info(lock->l_resource->lr_lvb_inode);
1043                 if (lli->lli_inode_magic == LLI_INODE_MAGIC) {
1044                         inode = igrab(lock->l_resource->lr_lvb_inode);
1045                 } else {
1046                         inode = lock->l_resource->lr_lvb_inode;
1047                         LDLM_DEBUG_LIMIT(inode->i_state & I_FREEING ?  D_INFO :
1048                                          D_WARNING, lock, "lr_lvb_inode %p is bogus: magic %08x",
1049                                          lock->l_resource->lr_lvb_inode,
1050                                          lli->lli_inode_magic);
1051                         inode = NULL;
1052                 }
1053         }
1054         unlock_res_and_lock(lock);
1055         return inode;
1056 }
1057
1058 void ll_dir_clear_lsm_md(struct inode *inode)
1059 {
1060         struct ll_inode_info *lli = ll_i2info(inode);
1061
1062         LASSERT(S_ISDIR(inode->i_mode));
1063
1064         if (lli->lli_lsm_md) {
1065                 lmv_free_memmd(lli->lli_lsm_md);
1066                 lli->lli_lsm_md = NULL;
1067         }
1068 }
1069
1070 static struct inode *ll_iget_anon_dir(struct super_block *sb,
1071                                       const struct lu_fid *fid,
1072                                       struct lustre_md *md)
1073 {
1074         struct ll_sb_info *sbi = ll_s2sbi(sb);
1075         struct mdt_body *body = md->body;
1076         struct inode *inode;
1077         ino_t ino;
1078
1079         ino = cl_fid_build_ino(fid, sbi->ll_flags & LL_SBI_32BIT_API);
1080         inode = iget_locked(sb, ino);
1081         if (!inode) {
1082                 CERROR("%s: failed get simple inode "DFID": rc = -ENOENT\n",
1083                        ll_get_fsname(sb, NULL, 0), PFID(fid));
1084                 return ERR_PTR(-ENOENT);
1085         }
1086
1087         if (inode->i_state & I_NEW) {
1088                 struct ll_inode_info *lli = ll_i2info(inode);
1089                 struct lmv_stripe_md *lsm = md->lmv;
1090
1091                 inode->i_mode = (inode->i_mode & ~S_IFMT) |
1092                                 (body->mbo_mode & S_IFMT);
1093                 LASSERTF(S_ISDIR(inode->i_mode), "Not slave inode "DFID"\n",
1094                          PFID(fid));
1095
1096                 LTIME_S(inode->i_mtime) = 0;
1097                 LTIME_S(inode->i_atime) = 0;
1098                 LTIME_S(inode->i_ctime) = 0;
1099                 inode->i_rdev = 0;
1100
1101                 inode->i_op = &ll_dir_inode_operations;
1102                 inode->i_fop = &ll_dir_operations;
1103                 lli->lli_fid = *fid;
1104                 ll_lli_init(lli);
1105
1106                 LASSERT(lsm);
1107                 /* master object FID */
1108                 lli->lli_pfid = body->mbo_fid1;
1109                 CDEBUG(D_INODE, "lli %p slave "DFID" master "DFID"\n",
1110                        lli, PFID(fid), PFID(&lli->lli_pfid));
1111                 unlock_new_inode(inode);
1112         }
1113
1114         return inode;
1115 }
1116
1117 static int ll_init_lsm_md(struct inode *inode, struct lustre_md *md)
1118 {
1119         struct lmv_stripe_md *lsm = md->lmv;
1120         struct lu_fid *fid;
1121         int i;
1122
1123         LASSERT(lsm);
1124         /*
1125          * XXX sigh, this lsm_root initialization should be in
1126          * LMV layer, but it needs ll_iget right now, so we
1127          * put this here right now.
1128          */
1129         for (i = 0; i < lsm->lsm_md_stripe_count; i++) {
1130                 fid = &lsm->lsm_md_oinfo[i].lmo_fid;
1131                 LASSERT(!lsm->lsm_md_oinfo[i].lmo_root);
1132                 /* Unfortunately ll_iget will call ll_update_inode,
1133                  * where the initialization of slave inode is slightly
1134                  * different, so it reset lsm_md to NULL to avoid
1135                  * initializing lsm for slave inode.
1136                  */
1137                 /* For migrating inode, master stripe and master object will
1138                  * be same, so we only need assign this inode
1139                  */
1140                 if (lsm->lsm_md_hash_type & LMV_HASH_FLAG_MIGRATION && !i)
1141                         lsm->lsm_md_oinfo[i].lmo_root = inode;
1142                 else
1143                         lsm->lsm_md_oinfo[i].lmo_root =
1144                                 ll_iget_anon_dir(inode->i_sb, fid, md);
1145                 if (IS_ERR(lsm->lsm_md_oinfo[i].lmo_root)) {
1146                         int rc = PTR_ERR(lsm->lsm_md_oinfo[i].lmo_root);
1147
1148                         lsm->lsm_md_oinfo[i].lmo_root = NULL;
1149                         return rc;
1150                 }
1151         }
1152
1153         return 0;
1154 }
1155
1156 static inline int lli_lsm_md_eq(const struct lmv_stripe_md *lsm_md1,
1157                                 const struct lmv_stripe_md *lsm_md2)
1158 {
1159         return lsm_md1->lsm_md_magic == lsm_md2->lsm_md_magic &&
1160                lsm_md1->lsm_md_stripe_count == lsm_md2->lsm_md_stripe_count &&
1161                lsm_md1->lsm_md_master_mdt_index ==
1162                         lsm_md2->lsm_md_master_mdt_index &&
1163                lsm_md1->lsm_md_hash_type == lsm_md2->lsm_md_hash_type &&
1164                lsm_md1->lsm_md_layout_version ==
1165                         lsm_md2->lsm_md_layout_version &&
1166                !strcmp(lsm_md1->lsm_md_pool_name,
1167                        lsm_md2->lsm_md_pool_name);
1168 }
1169
1170 static int ll_update_lsm_md(struct inode *inode, struct lustre_md *md)
1171 {
1172         struct ll_inode_info *lli = ll_i2info(inode);
1173         struct lmv_stripe_md *lsm = md->lmv;
1174         int rc;
1175
1176         LASSERT(S_ISDIR(inode->i_mode));
1177         CDEBUG(D_INODE, "update lsm %p of "DFID"\n", lli->lli_lsm_md,
1178                PFID(ll_inode2fid(inode)));
1179
1180         /* no striped information from request. */
1181         if (!lsm) {
1182                 if (!lli->lli_lsm_md) {
1183                         return 0;
1184                 } else if (lli->lli_lsm_md->lsm_md_hash_type &
1185                            LMV_HASH_FLAG_MIGRATION) {
1186                         /*
1187                          * migration is done, the temporay MIGRATE layout has
1188                          * been removed
1189                          */
1190                         CDEBUG(D_INODE, DFID" finish migration.\n",
1191                                PFID(ll_inode2fid(inode)));
1192                         lmv_free_memmd(lli->lli_lsm_md);
1193                         lli->lli_lsm_md = NULL;
1194                         return 0;
1195                 } else {
1196                         /*
1197                          * The lustre_md from req does not include stripeEA,
1198                          * see ll_md_setattr
1199                          */
1200                         return 0;
1201                 }
1202         }
1203
1204         /* set the directory layout */
1205         if (!lli->lli_lsm_md) {
1206                 struct cl_attr *attr;
1207
1208                 rc = ll_init_lsm_md(inode, md);
1209                 if (rc)
1210                         return rc;
1211
1212                 /*
1213                  * set lsm_md to NULL, so the following free lustre_md
1214                  * will not free this lsm
1215                  */
1216                 md->lmv = NULL;
1217                 lli->lli_lsm_md = lsm;
1218
1219                 attr = kzalloc(sizeof(*attr), GFP_NOFS);
1220                 if (!attr)
1221                         return -ENOMEM;
1222
1223                 /* validate the lsm */
1224                 rc = md_merge_attr(ll_i2mdexp(inode), lsm, attr,
1225                                    ll_md_blocking_ast);
1226                 if (rc) {
1227                         kfree(attr);
1228                         return rc;
1229                 }
1230
1231                 if (md->body->mbo_valid & OBD_MD_FLNLINK)
1232                         md->body->mbo_nlink = attr->cat_nlink;
1233                 if (md->body->mbo_valid & OBD_MD_FLSIZE)
1234                         md->body->mbo_size = attr->cat_size;
1235                 if (md->body->mbo_valid & OBD_MD_FLATIME)
1236                         md->body->mbo_atime = attr->cat_atime;
1237                 if (md->body->mbo_valid & OBD_MD_FLCTIME)
1238                         md->body->mbo_ctime = attr->cat_ctime;
1239                 if (md->body->mbo_valid & OBD_MD_FLMTIME)
1240                         md->body->mbo_mtime = attr->cat_mtime;
1241
1242                 kfree(attr);
1243
1244                 CDEBUG(D_INODE, "Set lsm %p magic %x to "DFID"\n", lsm,
1245                        lsm->lsm_md_magic, PFID(ll_inode2fid(inode)));
1246                 return 0;
1247         }
1248
1249         /* Compare the old and new stripe information */
1250         if (!lsm_md_eq(lli->lli_lsm_md, lsm)) {
1251                 struct lmv_stripe_md *old_lsm = lli->lli_lsm_md;
1252                 int idx;
1253
1254                 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",
1255                        ll_get_fsname(inode->i_sb, NULL, 0), PFID(&lli->lli_fid),
1256                        inode, lsm, old_lsm,
1257                        lsm->lsm_md_magic, old_lsm->lsm_md_magic,
1258                        lsm->lsm_md_stripe_count,
1259                        old_lsm->lsm_md_stripe_count,
1260                        lsm->lsm_md_master_mdt_index,
1261                        old_lsm->lsm_md_master_mdt_index,
1262                        lsm->lsm_md_hash_type, old_lsm->lsm_md_hash_type,
1263                        lsm->lsm_md_layout_version,
1264                        old_lsm->lsm_md_layout_version,
1265                        lsm->lsm_md_pool_name,
1266                        old_lsm->lsm_md_pool_name);
1267
1268                 for (idx = 0; idx < old_lsm->lsm_md_stripe_count; idx++) {
1269                         CERROR("%s: sub FIDs in old lsm idx %d, old: "DFID"\n",
1270                                ll_get_fsname(inode->i_sb, NULL, 0), idx,
1271                                PFID(&old_lsm->lsm_md_oinfo[idx].lmo_fid));
1272                 }
1273
1274                 for (idx = 0; idx < lsm->lsm_md_stripe_count; idx++) {
1275                         CERROR("%s: sub FIDs in new lsm idx %d, new: "DFID"\n",
1276                                ll_get_fsname(inode->i_sb, NULL, 0), idx,
1277                                PFID(&lsm->lsm_md_oinfo[idx].lmo_fid));
1278                 }
1279
1280                 return -EIO;
1281         }
1282
1283         return 0;
1284 }
1285
1286 void ll_clear_inode(struct inode *inode)
1287 {
1288         struct ll_inode_info *lli = ll_i2info(inode);
1289         struct ll_sb_info *sbi = ll_i2sbi(inode);
1290
1291         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
1292                PFID(ll_inode2fid(inode)), inode);
1293
1294         if (S_ISDIR(inode->i_mode)) {
1295                 /* these should have been cleared in ll_file_release */
1296                 LASSERT(!lli->lli_opendir_key);
1297                 LASSERT(!lli->lli_sai);
1298                 LASSERT(lli->lli_opendir_pid == 0);
1299         }
1300
1301         md_null_inode(sbi->ll_md_exp, ll_inode2fid(inode));
1302
1303         LASSERT(!lli->lli_open_fd_write_count);
1304         LASSERT(!lli->lli_open_fd_read_count);
1305         LASSERT(!lli->lli_open_fd_exec_count);
1306
1307         if (lli->lli_mds_write_och)
1308                 ll_md_real_close(inode, FMODE_WRITE);
1309         if (lli->lli_mds_exec_och)
1310                 ll_md_real_close(inode, FMODE_EXEC);
1311         if (lli->lli_mds_read_och)
1312                 ll_md_real_close(inode, FMODE_READ);
1313
1314         if (S_ISLNK(inode->i_mode)) {
1315                 kfree(lli->lli_symlink_name);
1316                 lli->lli_symlink_name = NULL;
1317         }
1318
1319         ll_xattr_cache_destroy(inode);
1320
1321 #ifdef CONFIG_FS_POSIX_ACL
1322         if (lli->lli_posix_acl) {
1323                 posix_acl_release(lli->lli_posix_acl);
1324                 lli->lli_posix_acl = NULL;
1325         }
1326 #endif
1327         lli->lli_inode_magic = LLI_INODE_DEAD;
1328
1329         if (S_ISDIR(inode->i_mode))
1330                 ll_dir_clear_lsm_md(inode);
1331         if (S_ISREG(inode->i_mode) && !is_bad_inode(inode))
1332                 LASSERT(list_empty(&lli->lli_agl_list));
1333
1334         /*
1335          * XXX This has to be done before lsm is freed below, because
1336          * cl_object still uses inode lsm.
1337          */
1338         cl_inode_fini(inode);
1339 }
1340
1341 #define TIMES_SET_FLAGS (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)
1342
1343 static int ll_md_setattr(struct dentry *dentry, struct md_op_data *op_data)
1344 {
1345         struct lustre_md md;
1346         struct inode *inode = d_inode(dentry);
1347         struct ll_sb_info *sbi = ll_i2sbi(inode);
1348         struct ptlrpc_request *request = NULL;
1349         int rc, ia_valid;
1350
1351         op_data = ll_prep_md_op_data(op_data, inode, NULL, NULL, 0, 0,
1352                                      LUSTRE_OPC_ANY, NULL);
1353         if (IS_ERR(op_data))
1354                 return PTR_ERR(op_data);
1355
1356         rc = md_setattr(sbi->ll_md_exp, op_data, NULL, 0, &request);
1357         if (rc) {
1358                 ptlrpc_req_finished(request);
1359                 if (rc == -ENOENT) {
1360                         clear_nlink(inode);
1361                         /* Unlinked special device node? Or just a race?
1362                          * Pretend we did everything.
1363                          */
1364                         if (!S_ISREG(inode->i_mode) &&
1365                             !S_ISDIR(inode->i_mode)) {
1366                                 ia_valid = op_data->op_attr.ia_valid;
1367                                 op_data->op_attr.ia_valid &= ~TIMES_SET_FLAGS;
1368                                 rc = simple_setattr(dentry, &op_data->op_attr);
1369                                 op_data->op_attr.ia_valid = ia_valid;
1370                         }
1371                 } else if (rc != -EPERM && rc != -EACCES && rc != -ETXTBSY) {
1372                         CERROR("md_setattr fails: rc = %d\n", rc);
1373                 }
1374                 return rc;
1375         }
1376
1377         rc = md_get_lustre_md(sbi->ll_md_exp, request, sbi->ll_dt_exp,
1378                               sbi->ll_md_exp, &md);
1379         if (rc) {
1380                 ptlrpc_req_finished(request);
1381                 return rc;
1382         }
1383
1384         ia_valid = op_data->op_attr.ia_valid;
1385         /* inode size will be in cl_setattr_ost, can't do it now since dirty
1386          * cache is not cleared yet.
1387          */
1388         op_data->op_attr.ia_valid &= ~(TIMES_SET_FLAGS | ATTR_SIZE);
1389         if (S_ISREG(inode->i_mode))
1390                 inode_lock(inode);
1391         rc = simple_setattr(dentry, &op_data->op_attr);
1392         if (S_ISREG(inode->i_mode))
1393                 inode_unlock(inode);
1394         op_data->op_attr.ia_valid = ia_valid;
1395
1396         rc = ll_update_inode(inode, &md);
1397         ptlrpc_req_finished(request);
1398
1399         return rc;
1400 }
1401
1402 /* If this inode has objects allocated to it (lsm != NULL), then the OST
1403  * object(s) determine the file size and mtime.  Otherwise, the MDS will
1404  * keep these values until such a time that objects are allocated for it.
1405  * We do the MDS operations first, as it is checking permissions for us.
1406  * We don't to the MDS RPC if there is nothing that we want to store there,
1407  * otherwise there is no harm in updating mtime/atime on the MDS if we are
1408  * going to do an RPC anyways.
1409  *
1410  * If we are doing a truncate, we will send the mtime and ctime updates
1411  * to the OST with the punch RPC, otherwise we do an explicit setattr RPC.
1412  * I don't believe it is possible to get e.g. ATTR_MTIME_SET and ATTR_SIZE
1413  * at the same time.
1414  *
1415  * In case of HSMimport, we only set attr on MDS.
1416  */
1417 int ll_setattr_raw(struct dentry *dentry, struct iattr *attr, bool hsm_import)
1418 {
1419         struct inode *inode = d_inode(dentry);
1420         struct ll_inode_info *lli = ll_i2info(inode);
1421         struct md_op_data *op_data = NULL;
1422         int rc = 0;
1423
1424         CDEBUG(D_VFSTRACE, "%s: setattr inode "DFID"(%p) from %llu to %llu, valid %x, hsm_import %d\n",
1425                ll_get_fsname(inode->i_sb, NULL, 0), PFID(&lli->lli_fid), inode,
1426                i_size_read(inode), attr->ia_size, attr->ia_valid, hsm_import);
1427
1428         if (attr->ia_valid & ATTR_SIZE) {
1429                 /* Check new size against VFS/VM file size limit and rlimit */
1430                 rc = inode_newsize_ok(inode, attr->ia_size);
1431                 if (rc)
1432                         return rc;
1433
1434                 /* The maximum Lustre file size is variable, based on the
1435                  * OST maximum object size and number of stripes.  This
1436                  * needs another check in addition to the VFS check above.
1437                  */
1438                 if (attr->ia_size > ll_file_maxbytes(inode)) {
1439                         CDEBUG(D_INODE, "file "DFID" too large %llu > %llu\n",
1440                                PFID(&lli->lli_fid), attr->ia_size,
1441                                ll_file_maxbytes(inode));
1442                         return -EFBIG;
1443                 }
1444
1445                 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
1446         }
1447
1448         /* POSIX: check before ATTR_*TIME_SET set (from setattr_prepare) */
1449         if (attr->ia_valid & TIMES_SET_FLAGS) {
1450                 if ((!uid_eq(current_fsuid(), inode->i_uid)) &&
1451                     !capable(CFS_CAP_FOWNER))
1452                         return -EPERM;
1453         }
1454
1455         /* We mark all of the fields "set" so MDS/OST does not re-set them */
1456         if (attr->ia_valid & ATTR_CTIME) {
1457                 attr->ia_ctime = CURRENT_TIME;
1458                 attr->ia_valid |= ATTR_CTIME_SET;
1459         }
1460         if (!(attr->ia_valid & ATTR_ATIME_SET) &&
1461             (attr->ia_valid & ATTR_ATIME)) {
1462                 attr->ia_atime = CURRENT_TIME;
1463                 attr->ia_valid |= ATTR_ATIME_SET;
1464         }
1465         if (!(attr->ia_valid & ATTR_MTIME_SET) &&
1466             (attr->ia_valid & ATTR_MTIME)) {
1467                 attr->ia_mtime = CURRENT_TIME;
1468                 attr->ia_valid |= ATTR_MTIME_SET;
1469         }
1470
1471         if (attr->ia_valid & (ATTR_MTIME | ATTR_CTIME))
1472                 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu, now = %llu\n",
1473                        LTIME_S(attr->ia_mtime), LTIME_S(attr->ia_ctime),
1474                        (s64)ktime_get_real_seconds());
1475
1476         if (S_ISREG(inode->i_mode))
1477                 inode_unlock(inode);
1478
1479         /*
1480          * We always do an MDS RPC, even if we're only changing the size;
1481          * only the MDS knows whether truncate() should fail with -ETXTBUSY
1482          */
1483         op_data = kzalloc(sizeof(*op_data), GFP_NOFS);
1484         if (!op_data) {
1485                 rc = -ENOMEM;
1486                 goto out;
1487         }
1488
1489         op_data->op_attr = *attr;
1490
1491         if (!hsm_import && attr->ia_valid & ATTR_SIZE) {
1492                 /*
1493                  * If we are changing file size, file content is
1494                  * modified, flag it.
1495                  */
1496                 attr->ia_valid |= MDS_OPEN_OWNEROVERRIDE;
1497                 op_data->op_bias |= MDS_DATA_MODIFIED;
1498         }
1499
1500         rc = ll_md_setattr(dentry, op_data);
1501         if (rc)
1502                 goto out;
1503
1504         if (!S_ISREG(inode->i_mode) || hsm_import) {
1505                 rc = 0;
1506                 goto out;
1507         }
1508
1509         if (attr->ia_valid & (ATTR_SIZE |
1510                               ATTR_ATIME | ATTR_ATIME_SET |
1511                               ATTR_MTIME | ATTR_MTIME_SET)) {
1512                 /* For truncate and utimes sending attributes to OSTs, setting
1513                  * mtime/atime to the past will be performed under PW [0:EOF]
1514                  * extent lock (new_size:EOF for truncate).  It may seem
1515                  * excessive to send mtime/atime updates to OSTs when not
1516                  * setting times to past, but it is necessary due to possible
1517                  * time de-synchronization between MDT inode and OST objects
1518                  */
1519                 rc = cl_setattr_ost(ll_i2info(inode)->lli_clob, attr, 0);
1520         }
1521
1522         /*
1523          * If the file was restored, it needs to set dirty flag.
1524          *
1525          * We've already sent MDS_DATA_MODIFIED flag in
1526          * ll_md_setattr() for truncate. However, the MDT refuses to
1527          * set the HS_DIRTY flag on released files, so we have to set
1528          * it again if the file has been restored. Please check how
1529          * LLIF_DATA_MODIFIED is set in vvp_io_setattr_fini().
1530          *
1531          * Please notice that if the file is not released, the previous
1532          * MDS_DATA_MODIFIED has taken effect and usually
1533          * LLIF_DATA_MODIFIED is not set(see vvp_io_setattr_fini()).
1534          * This way we can save an RPC for common open + trunc
1535          * operation.
1536          */
1537         if (test_and_clear_bit(LLIF_DATA_MODIFIED, &lli->lli_flags)) {
1538                 struct hsm_state_set hss = {
1539                         .hss_valid = HSS_SETMASK,
1540                         .hss_setmask = HS_DIRTY,
1541                 };
1542                 int rc2;
1543
1544                 rc2 = ll_hsm_state_set(inode, &hss);
1545                 if (rc2 < 0)
1546                         CERROR(DFID "HSM set dirty failed: rc2 = %d\n",
1547                                PFID(ll_inode2fid(inode)), rc2);
1548         }
1549
1550 out:
1551         if (op_data)
1552                 ll_finish_md_op_data(op_data);
1553
1554         if (S_ISREG(inode->i_mode)) {
1555                 inode_lock(inode);
1556                 if ((attr->ia_valid & ATTR_SIZE) && !hsm_import)
1557                         inode_dio_wait(inode);
1558         }
1559
1560         ll_stats_ops_tally(ll_i2sbi(inode), (attr->ia_valid & ATTR_SIZE) ?
1561                         LPROC_LL_TRUNC : LPROC_LL_SETATTR, 1);
1562
1563         return rc;
1564 }
1565
1566 int ll_setattr(struct dentry *de, struct iattr *attr)
1567 {
1568         int mode = d_inode(de)->i_mode;
1569
1570         if ((attr->ia_valid & (ATTR_CTIME | ATTR_SIZE | ATTR_MODE)) ==
1571                               (ATTR_CTIME | ATTR_SIZE | ATTR_MODE))
1572                 attr->ia_valid |= MDS_OPEN_OWNEROVERRIDE;
1573
1574         if (((attr->ia_valid & (ATTR_MODE | ATTR_FORCE | ATTR_SIZE)) ==
1575                                (ATTR_SIZE | ATTR_MODE)) &&
1576             (((mode & S_ISUID) && !(attr->ia_mode & S_ISUID)) ||
1577              (((mode & (S_ISGID | 0010)) == (S_ISGID | 0010)) &&
1578               !(attr->ia_mode & S_ISGID))))
1579                 attr->ia_valid |= ATTR_FORCE;
1580
1581         if ((attr->ia_valid & ATTR_MODE) &&
1582             (mode & S_ISUID) &&
1583             !(attr->ia_mode & S_ISUID) &&
1584             !(attr->ia_valid & ATTR_KILL_SUID))
1585                 attr->ia_valid |= ATTR_KILL_SUID;
1586
1587         if ((attr->ia_valid & ATTR_MODE) &&
1588             ((mode & (S_ISGID | 0010)) == (S_ISGID | 0010)) &&
1589             !(attr->ia_mode & S_ISGID) &&
1590             !(attr->ia_valid & ATTR_KILL_SGID))
1591                 attr->ia_valid |= ATTR_KILL_SGID;
1592
1593         return ll_setattr_raw(de, attr, false);
1594 }
1595
1596 int ll_statfs_internal(struct super_block *sb, struct obd_statfs *osfs,
1597                        __u64 max_age, __u32 flags)
1598 {
1599         struct ll_sb_info *sbi = ll_s2sbi(sb);
1600         struct obd_statfs obd_osfs;
1601         int rc;
1602
1603         rc = obd_statfs(NULL, sbi->ll_md_exp, osfs, max_age, flags);
1604         if (rc) {
1605                 CERROR("md_statfs fails: rc = %d\n", rc);
1606                 return rc;
1607         }
1608
1609         osfs->os_type = sb->s_magic;
1610
1611         CDEBUG(D_SUPER, "MDC blocks %llu/%llu objects %llu/%llu\n",
1612                osfs->os_bavail, osfs->os_blocks, osfs->os_ffree,
1613                osfs->os_files);
1614
1615         if (sbi->ll_flags & LL_SBI_LAZYSTATFS)
1616                 flags |= OBD_STATFS_NODELAY;
1617
1618         rc = obd_statfs_rqset(sbi->ll_dt_exp, &obd_osfs, max_age, flags);
1619         if (rc) {
1620                 CERROR("obd_statfs fails: rc = %d\n", rc);
1621                 return rc;
1622         }
1623
1624         CDEBUG(D_SUPER, "OSC blocks %llu/%llu objects %llu/%llu\n",
1625                obd_osfs.os_bavail, obd_osfs.os_blocks, obd_osfs.os_ffree,
1626                obd_osfs.os_files);
1627
1628         osfs->os_bsize = obd_osfs.os_bsize;
1629         osfs->os_blocks = obd_osfs.os_blocks;
1630         osfs->os_bfree = obd_osfs.os_bfree;
1631         osfs->os_bavail = obd_osfs.os_bavail;
1632
1633         /* If we don't have as many objects free on the OST as inodes
1634          * on the MDS, we reduce the total number of inodes to
1635          * compensate, so that the "inodes in use" number is correct.
1636          */
1637         if (obd_osfs.os_ffree < osfs->os_ffree) {
1638                 osfs->os_files = (osfs->os_files - osfs->os_ffree) +
1639                         obd_osfs.os_ffree;
1640                 osfs->os_ffree = obd_osfs.os_ffree;
1641         }
1642
1643         return rc;
1644 }
1645
1646 int ll_statfs(struct dentry *de, struct kstatfs *sfs)
1647 {
1648         struct super_block *sb = de->d_sb;
1649         struct obd_statfs osfs;
1650         int rc;
1651
1652         CDEBUG(D_VFSTRACE, "VFS Op: at %llu jiffies\n", get_jiffies_64());
1653         ll_stats_ops_tally(ll_s2sbi(sb), LPROC_LL_STAFS, 1);
1654
1655         /* Some amount of caching on the client is allowed */
1656         rc = ll_statfs_internal(sb, &osfs,
1657                                 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
1658                                 0);
1659         if (rc)
1660                 return rc;
1661
1662         statfs_unpack(sfs, &osfs);
1663
1664         /* We need to downshift for all 32-bit kernels, because we can't
1665          * tell if the kernel is being called via sys_statfs64() or not.
1666          * Stop before overflowing f_bsize - in which case it is better
1667          * to just risk EOVERFLOW if caller is using old sys_statfs().
1668          */
1669         if (sizeof(long) < 8) {
1670                 while (osfs.os_blocks > ~0UL && sfs->f_bsize < 0x40000000) {
1671                         sfs->f_bsize <<= 1;
1672
1673                         osfs.os_blocks >>= 1;
1674                         osfs.os_bfree >>= 1;
1675                         osfs.os_bavail >>= 1;
1676                 }
1677         }
1678
1679         sfs->f_blocks = osfs.os_blocks;
1680         sfs->f_bfree = osfs.os_bfree;
1681         sfs->f_bavail = osfs.os_bavail;
1682         sfs->f_fsid = ll_s2sbi(sb)->ll_fsid;
1683         return 0;
1684 }
1685
1686 void ll_inode_size_lock(struct inode *inode)
1687 {
1688         struct ll_inode_info *lli;
1689
1690         LASSERT(!S_ISDIR(inode->i_mode));
1691
1692         lli = ll_i2info(inode);
1693         mutex_lock(&lli->lli_size_mutex);
1694 }
1695
1696 void ll_inode_size_unlock(struct inode *inode)
1697 {
1698         struct ll_inode_info *lli;
1699
1700         lli = ll_i2info(inode);
1701         mutex_unlock(&lli->lli_size_mutex);
1702 }
1703
1704 int ll_update_inode(struct inode *inode, struct lustre_md *md)
1705 {
1706         struct ll_inode_info *lli = ll_i2info(inode);
1707         struct mdt_body *body = md->body;
1708         struct ll_sb_info *sbi = ll_i2sbi(inode);
1709
1710         if (body->mbo_valid & OBD_MD_FLEASIZE)
1711                 cl_file_inode_init(inode, md);
1712
1713         if (S_ISDIR(inode->i_mode)) {
1714                 int rc;
1715
1716                 rc = ll_update_lsm_md(inode, md);
1717                 if (rc)
1718                         return rc;
1719         }
1720
1721 #ifdef CONFIG_FS_POSIX_ACL
1722         if (body->mbo_valid & OBD_MD_FLACL) {
1723                 spin_lock(&lli->lli_lock);
1724                 if (lli->lli_posix_acl)
1725                         posix_acl_release(lli->lli_posix_acl);
1726                 lli->lli_posix_acl = md->posix_acl;
1727                 spin_unlock(&lli->lli_lock);
1728         }
1729 #endif
1730         inode->i_ino = cl_fid_build_ino(&body->mbo_fid1,
1731                                         sbi->ll_flags & LL_SBI_32BIT_API);
1732         inode->i_generation = cl_fid_build_gen(&body->mbo_fid1);
1733
1734         if (body->mbo_valid & OBD_MD_FLATIME) {
1735                 if (body->mbo_atime > LTIME_S(inode->i_atime))
1736                         LTIME_S(inode->i_atime) = body->mbo_atime;
1737                 lli->lli_atime = body->mbo_atime;
1738         }
1739         if (body->mbo_valid & OBD_MD_FLMTIME) {
1740                 if (body->mbo_mtime > LTIME_S(inode->i_mtime)) {
1741                         CDEBUG(D_INODE, "setting ino %lu mtime from %lu to %llu\n",
1742                                inode->i_ino, LTIME_S(inode->i_mtime),
1743                                body->mbo_mtime);
1744                         LTIME_S(inode->i_mtime) = body->mbo_mtime;
1745                 }
1746                 lli->lli_mtime = body->mbo_mtime;
1747         }
1748         if (body->mbo_valid & OBD_MD_FLCTIME) {
1749                 if (body->mbo_ctime > LTIME_S(inode->i_ctime))
1750                         LTIME_S(inode->i_ctime) = body->mbo_ctime;
1751                 lli->lli_ctime = body->mbo_ctime;
1752         }
1753         if (body->mbo_valid & OBD_MD_FLMODE)
1754                 inode->i_mode = (inode->i_mode & S_IFMT) |
1755                                 (body->mbo_mode & ~S_IFMT);
1756         if (body->mbo_valid & OBD_MD_FLTYPE)
1757                 inode->i_mode = (inode->i_mode & ~S_IFMT) |
1758                                 (body->mbo_mode & S_IFMT);
1759         LASSERT(inode->i_mode != 0);
1760         if (S_ISREG(inode->i_mode))
1761                 inode->i_blkbits = min(PTLRPC_MAX_BRW_BITS + 1,
1762                                        LL_MAX_BLKSIZE_BITS);
1763         else
1764                 inode->i_blkbits = inode->i_sb->s_blocksize_bits;
1765         if (body->mbo_valid & OBD_MD_FLUID)
1766                 inode->i_uid = make_kuid(&init_user_ns, body->mbo_uid);
1767         if (body->mbo_valid & OBD_MD_FLGID)
1768                 inode->i_gid = make_kgid(&init_user_ns, body->mbo_gid);
1769         if (body->mbo_valid & OBD_MD_FLFLAGS)
1770                 inode->i_flags = ll_ext_to_inode_flags(body->mbo_flags);
1771         if (body->mbo_valid & OBD_MD_FLNLINK)
1772                 set_nlink(inode, body->mbo_nlink);
1773         if (body->mbo_valid & OBD_MD_FLRDEV)
1774                 inode->i_rdev = old_decode_dev(body->mbo_rdev);
1775
1776         if (body->mbo_valid & OBD_MD_FLID) {
1777                 /* FID shouldn't be changed! */
1778                 if (fid_is_sane(&lli->lli_fid)) {
1779                         LASSERTF(lu_fid_eq(&lli->lli_fid, &body->mbo_fid1),
1780                                  "Trying to change FID "DFID" to the "DFID", inode "DFID"(%p)\n",
1781                                  PFID(&lli->lli_fid), PFID(&body->mbo_fid1),
1782                                  PFID(ll_inode2fid(inode)), inode);
1783                 } else {
1784                         lli->lli_fid = body->mbo_fid1;
1785                 }
1786         }
1787
1788         LASSERT(fid_seq(&lli->lli_fid) != 0);
1789
1790         if (body->mbo_valid & OBD_MD_FLSIZE) {
1791                 i_size_write(inode, body->mbo_size);
1792
1793                 CDEBUG(D_VFSTRACE, "inode=" DFID ", updating i_size %llu\n",
1794                        PFID(ll_inode2fid(inode)),
1795                        (unsigned long long)body->mbo_size);
1796
1797                 if (body->mbo_valid & OBD_MD_FLBLOCKS)
1798                         inode->i_blocks = body->mbo_blocks;
1799         }
1800
1801         if (body->mbo_valid & OBD_MD_TSTATE) {
1802                 if (body->mbo_t_state & MS_RESTORE)
1803                         set_bit(LLIF_FILE_RESTORING, &lli->lli_flags);
1804         }
1805
1806         return 0;
1807 }
1808
1809 int ll_read_inode2(struct inode *inode, void *opaque)
1810 {
1811         struct lustre_md *md = opaque;
1812         struct ll_inode_info *lli = ll_i2info(inode);
1813         int rc;
1814
1815         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
1816                PFID(&lli->lli_fid), inode);
1817
1818         /* Core attributes from the MDS first.  This is a new inode, and
1819          * the VFS doesn't zero times in the core inode so we have to do
1820          * it ourselves.  They will be overwritten by either MDS or OST
1821          * attributes - we just need to make sure they aren't newer.
1822          */
1823         LTIME_S(inode->i_mtime) = 0;
1824         LTIME_S(inode->i_atime) = 0;
1825         LTIME_S(inode->i_ctime) = 0;
1826         inode->i_rdev = 0;
1827         rc = ll_update_inode(inode, md);
1828         if (rc)
1829                 return rc;
1830
1831         /* OIDEBUG(inode); */
1832
1833         if (S_ISREG(inode->i_mode)) {
1834                 struct ll_sb_info *sbi = ll_i2sbi(inode);
1835
1836                 inode->i_op = &ll_file_inode_operations;
1837                 inode->i_fop = sbi->ll_fop;
1838                 inode->i_mapping->a_ops = (struct address_space_operations *)&ll_aops;
1839         } else if (S_ISDIR(inode->i_mode)) {
1840                 inode->i_op = &ll_dir_inode_operations;
1841                 inode->i_fop = &ll_dir_operations;
1842         } else if (S_ISLNK(inode->i_mode)) {
1843                 inode->i_op = &ll_fast_symlink_inode_operations;
1844         } else {
1845                 inode->i_op = &ll_special_inode_operations;
1846
1847                 init_special_inode(inode, inode->i_mode,
1848                                    inode->i_rdev);
1849         }
1850
1851         return 0;
1852 }
1853
1854 void ll_delete_inode(struct inode *inode)
1855 {
1856         struct ll_inode_info *lli = ll_i2info(inode);
1857
1858         if (S_ISREG(inode->i_mode) && lli->lli_clob)
1859                 /* discard all dirty pages before truncating them, required by
1860                  * osc_extent implementation at LU-1030.
1861                  */
1862                 cl_sync_file_range(inode, 0, OBD_OBJECT_EOF,
1863                                    CL_FSYNC_LOCAL, 1);
1864
1865         truncate_inode_pages_final(&inode->i_data);
1866
1867         LASSERTF(!inode->i_data.nrpages,
1868                  "inode=" DFID "(%p) nrpages=%lu, see http://jira.whamcloud.com/browse/LU-118\n",
1869                  PFID(ll_inode2fid(inode)), inode, inode->i_data.nrpages);
1870
1871         ll_clear_inode(inode);
1872         clear_inode(inode);
1873 }
1874
1875 int ll_iocontrol(struct inode *inode, struct file *file,
1876                  unsigned int cmd, unsigned long arg)
1877 {
1878         struct ll_sb_info *sbi = ll_i2sbi(inode);
1879         struct ptlrpc_request *req = NULL;
1880         int rc, flags = 0;
1881
1882         switch (cmd) {
1883         case FSFILT_IOC_GETFLAGS: {
1884                 struct mdt_body *body;
1885                 struct md_op_data *op_data;
1886
1887                 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL,
1888                                              0, 0, LUSTRE_OPC_ANY,
1889                                              NULL);
1890                 if (IS_ERR(op_data))
1891                         return PTR_ERR(op_data);
1892
1893                 op_data->op_valid = OBD_MD_FLFLAGS;
1894                 rc = md_getattr(sbi->ll_md_exp, op_data, &req);
1895                 ll_finish_md_op_data(op_data);
1896                 if (rc) {
1897                         CERROR("%s: failure inode "DFID": rc = %d\n",
1898                                sbi->ll_md_exp->exp_obd->obd_name,
1899                                PFID(ll_inode2fid(inode)), rc);
1900                         return -abs(rc);
1901                 }
1902
1903                 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
1904
1905                 flags = body->mbo_flags;
1906
1907                 ptlrpc_req_finished(req);
1908
1909                 return put_user(flags, (int __user *)arg);
1910         }
1911         case FSFILT_IOC_SETFLAGS: {
1912                 struct md_op_data *op_data;
1913                 struct cl_object *obj;
1914                 struct iattr *attr;
1915
1916                 if (get_user(flags, (int __user *)arg))
1917                         return -EFAULT;
1918
1919                 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
1920                                              LUSTRE_OPC_ANY, NULL);
1921                 if (IS_ERR(op_data))
1922                         return PTR_ERR(op_data);
1923
1924                 op_data->op_attr_flags = flags;
1925                 op_data->op_attr.ia_valid |= ATTR_ATTR_FLAG;
1926                 rc = md_setattr(sbi->ll_md_exp, op_data, NULL, 0, &req);
1927                 ll_finish_md_op_data(op_data);
1928                 ptlrpc_req_finished(req);
1929                 if (rc)
1930                         return rc;
1931
1932                 inode->i_flags = ll_ext_to_inode_flags(flags);
1933
1934                 obj = ll_i2info(inode)->lli_clob;
1935                 if (!obj)
1936                         return 0;
1937
1938                 attr = kzalloc(sizeof(*attr), GFP_NOFS);
1939                 if (!attr)
1940                         return -ENOMEM;
1941
1942                 attr->ia_valid = ATTR_ATTR_FLAG;
1943                 rc = cl_setattr_ost(obj, attr, flags);
1944                 kfree(attr);
1945                 return rc;
1946         }
1947         default:
1948                 return -ENOSYS;
1949         }
1950
1951         return 0;
1952 }
1953
1954 int ll_flush_ctx(struct inode *inode)
1955 {
1956         struct ll_sb_info  *sbi = ll_i2sbi(inode);
1957
1958         CDEBUG(D_SEC, "flush context for user %d\n",
1959                from_kuid(&init_user_ns, current_uid()));
1960
1961         obd_set_info_async(NULL, sbi->ll_md_exp,
1962                            sizeof(KEY_FLUSH_CTX), KEY_FLUSH_CTX,
1963                            0, NULL, NULL);
1964         obd_set_info_async(NULL, sbi->ll_dt_exp,
1965                            sizeof(KEY_FLUSH_CTX), KEY_FLUSH_CTX,
1966                            0, NULL, NULL);
1967         return 0;
1968 }
1969
1970 /* umount -f client means force down, don't save state */
1971 void ll_umount_begin(struct super_block *sb)
1972 {
1973         struct ll_sb_info *sbi = ll_s2sbi(sb);
1974         struct obd_device *obd;
1975         struct obd_ioctl_data *ioc_data;
1976         wait_queue_head_t waitq;
1977         struct l_wait_info lwi;
1978
1979         CDEBUG(D_VFSTRACE, "VFS Op: superblock %p count %d active %d\n", sb,
1980                sb->s_count, atomic_read(&sb->s_active));
1981
1982         obd = class_exp2obd(sbi->ll_md_exp);
1983         if (!obd) {
1984                 CERROR("Invalid MDC connection handle %#llx\n",
1985                        sbi->ll_md_exp->exp_handle.h_cookie);
1986                 return;
1987         }
1988         obd->obd_force = 1;
1989
1990         obd = class_exp2obd(sbi->ll_dt_exp);
1991         if (!obd) {
1992                 CERROR("Invalid LOV connection handle %#llx\n",
1993                        sbi->ll_dt_exp->exp_handle.h_cookie);
1994                 return;
1995         }
1996         obd->obd_force = 1;
1997
1998         ioc_data = kzalloc(sizeof(*ioc_data), GFP_NOFS);
1999         if (ioc_data) {
2000                 obd_iocontrol(IOC_OSC_SET_ACTIVE, sbi->ll_md_exp,
2001                               sizeof(*ioc_data), ioc_data, NULL);
2002
2003                 obd_iocontrol(IOC_OSC_SET_ACTIVE, sbi->ll_dt_exp,
2004                               sizeof(*ioc_data), ioc_data, NULL);
2005
2006                 kfree(ioc_data);
2007         }
2008
2009         /* Really, we'd like to wait until there are no requests outstanding,
2010          * and then continue. For now, we just periodically checking for vfs
2011          * to decrement mnt_cnt and hope to finish it within 10sec.
2012          */
2013         init_waitqueue_head(&waitq);
2014         lwi = LWI_TIMEOUT_INTERVAL(cfs_time_seconds(10),
2015                                    cfs_time_seconds(1), NULL, NULL);
2016         l_wait_event(waitq, may_umount(sbi->ll_mnt.mnt), &lwi);
2017
2018         schedule();
2019 }
2020
2021 int ll_remount_fs(struct super_block *sb, int *flags, char *data)
2022 {
2023         struct ll_sb_info *sbi = ll_s2sbi(sb);
2024         char *profilenm = get_profile_name(sb);
2025         int err;
2026         __u32 read_only;
2027
2028         if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY)) {
2029                 read_only = *flags & MS_RDONLY;
2030                 err = obd_set_info_async(NULL, sbi->ll_md_exp,
2031                                          sizeof(KEY_READ_ONLY),
2032                                          KEY_READ_ONLY, sizeof(read_only),
2033                                          &read_only, NULL);
2034                 if (err) {
2035                         LCONSOLE_WARN("Failed to remount %s %s (%d)\n",
2036                                       profilenm, read_only ?
2037                                       "read-only" : "read-write", err);
2038                         return err;
2039                 }
2040
2041                 if (read_only)
2042                         sb->s_flags |= MS_RDONLY;
2043                 else
2044                         sb->s_flags &= ~MS_RDONLY;
2045
2046                 if (sbi->ll_flags & LL_SBI_VERBOSE)
2047                         LCONSOLE_WARN("Remounted %s %s\n", profilenm,
2048                                       read_only ?  "read-only" : "read-write");
2049         }
2050         return 0;
2051 }
2052
2053 /**
2054  * Cleanup the open handle that is cached on MDT-side.
2055  *
2056  * For open case, the client side open handling thread may hit error
2057  * after the MDT grant the open. Under such case, the client should
2058  * send close RPC to the MDT as cleanup; otherwise, the open handle
2059  * on the MDT will be leaked there until the client umount or evicted.
2060  *
2061  * In further, if someone unlinked the file, because the open handle
2062  * holds the reference on such file/object, then it will block the
2063  * subsequent threads that want to locate such object via FID.
2064  *
2065  * \param[in] sb        super block for this file-system
2066  * \param[in] open_req  pointer to the original open request
2067  */
2068 void ll_open_cleanup(struct super_block *sb, struct ptlrpc_request *open_req)
2069 {
2070         struct mdt_body                 *body;
2071         struct md_op_data               *op_data;
2072         struct ptlrpc_request           *close_req = NULL;
2073         struct obd_export               *exp       = ll_s2sbi(sb)->ll_md_exp;
2074
2075         body = req_capsule_server_get(&open_req->rq_pill, &RMF_MDT_BODY);
2076         op_data = kzalloc(sizeof(*op_data), GFP_NOFS);
2077         if (!op_data)
2078                 return;
2079
2080         op_data->op_fid1 = body->mbo_fid1;
2081         op_data->op_handle = body->mbo_handle;
2082         op_data->op_mod_time = get_seconds();
2083         md_close(exp, op_data, NULL, &close_req);
2084         ptlrpc_req_finished(close_req);
2085         ll_finish_md_op_data(op_data);
2086 }
2087
2088 int ll_prep_inode(struct inode **inode, struct ptlrpc_request *req,
2089                   struct super_block *sb, struct lookup_intent *it)
2090 {
2091         struct ll_sb_info *sbi = NULL;
2092         struct lustre_md md = { NULL };
2093         int rc;
2094
2095         LASSERT(*inode || sb);
2096         sbi = sb ? ll_s2sbi(sb) : ll_i2sbi(*inode);
2097         rc = md_get_lustre_md(sbi->ll_md_exp, req, sbi->ll_dt_exp,
2098                               sbi->ll_md_exp, &md);
2099         if (rc)
2100                 goto cleanup;
2101
2102         if (*inode) {
2103                 rc = ll_update_inode(*inode, &md);
2104                 if (rc)
2105                         goto out;
2106         } else {
2107                 LASSERT(sb);
2108
2109                 /*
2110                  * At this point server returns to client's same fid as client
2111                  * generated for creating. So using ->fid1 is okay here.
2112                  */
2113                 if (!fid_is_sane(&md.body->mbo_fid1)) {
2114                         CERROR("%s: Fid is insane " DFID "\n",
2115                                ll_get_fsname(sb, NULL, 0),
2116                                PFID(&md.body->mbo_fid1));
2117                         rc = -EINVAL;
2118                         goto out;
2119                 }
2120
2121                 *inode = ll_iget(sb, cl_fid_build_ino(&md.body->mbo_fid1,
2122                                              sbi->ll_flags & LL_SBI_32BIT_API),
2123                                  &md);
2124                 if (IS_ERR(*inode)) {
2125 #ifdef CONFIG_FS_POSIX_ACL
2126                         if (md.posix_acl) {
2127                                 posix_acl_release(md.posix_acl);
2128                                 md.posix_acl = NULL;
2129                         }
2130 #endif
2131                         rc = -ENOMEM;
2132                         CERROR("new_inode -fatal: rc %d\n", rc);
2133                         goto out;
2134                 }
2135         }
2136
2137         /* Handling piggyback layout lock.
2138          * Layout lock can be piggybacked by getattr and open request.
2139          * The lsm can be applied to inode only if it comes with a layout lock
2140          * otherwise correct layout may be overwritten, for example:
2141          * 1. proc1: mdt returns a lsm but not granting layout
2142          * 2. layout was changed by another client
2143          * 3. proc2: refresh layout and layout lock granted
2144          * 4. proc1: to apply a stale layout
2145          */
2146         if (it && it->it_lock_mode != 0) {
2147                 struct lustre_handle lockh;
2148                 struct ldlm_lock *lock;
2149
2150                 lockh.cookie = it->it_lock_handle;
2151                 lock = ldlm_handle2lock(&lockh);
2152                 LASSERT(lock);
2153                 if (ldlm_has_layout(lock)) {
2154                         struct cl_object_conf conf;
2155
2156                         memset(&conf, 0, sizeof(conf));
2157                         conf.coc_opc = OBJECT_CONF_SET;
2158                         conf.coc_inode = *inode;
2159                         conf.coc_lock = lock;
2160                         conf.u.coc_layout = md.layout;
2161                         (void)ll_layout_conf(*inode, &conf);
2162                 }
2163                 LDLM_LOCK_PUT(lock);
2164         }
2165
2166 out:
2167         md_free_lustre_md(sbi->ll_md_exp, &md);
2168 cleanup:
2169         if (rc != 0 && it && it->it_op & IT_OPEN)
2170                 ll_open_cleanup(sb ? sb : (*inode)->i_sb, req);
2171
2172         return rc;
2173 }
2174
2175 int ll_obd_statfs(struct inode *inode, void __user *arg)
2176 {
2177         struct ll_sb_info *sbi = NULL;
2178         struct obd_export *exp;
2179         char *buf = NULL;
2180         struct obd_ioctl_data *data = NULL;
2181         __u32 type;
2182         int len = 0, rc;
2183
2184         if (!inode) {
2185                 rc = -EINVAL;
2186                 goto out_statfs;
2187         }
2188
2189         sbi = ll_i2sbi(inode);
2190         if (!sbi) {
2191                 rc = -EINVAL;
2192                 goto out_statfs;
2193         }
2194
2195         rc = obd_ioctl_getdata(&buf, &len, arg);
2196         if (rc)
2197                 goto out_statfs;
2198
2199         data = (void *)buf;
2200         if (!data->ioc_inlbuf1 || !data->ioc_inlbuf2 ||
2201             !data->ioc_pbuf1 || !data->ioc_pbuf2) {
2202                 rc = -EINVAL;
2203                 goto out_statfs;
2204         }
2205
2206         if (data->ioc_inllen1 != sizeof(__u32) ||
2207             data->ioc_inllen2 != sizeof(__u32) ||
2208             data->ioc_plen1 != sizeof(struct obd_statfs) ||
2209             data->ioc_plen2 != sizeof(struct obd_uuid)) {
2210                 rc = -EINVAL;
2211                 goto out_statfs;
2212         }
2213
2214         memcpy(&type, data->ioc_inlbuf1, sizeof(__u32));
2215         if (type & LL_STATFS_LMV) {
2216                 exp = sbi->ll_md_exp;
2217         } else if (type & LL_STATFS_LOV) {
2218                 exp = sbi->ll_dt_exp;
2219         } else {
2220                 rc = -ENODEV;
2221                 goto out_statfs;
2222         }
2223
2224         rc = obd_iocontrol(IOC_OBD_STATFS, exp, len, buf, NULL);
2225         if (rc)
2226                 goto out_statfs;
2227 out_statfs:
2228         if (buf)
2229                 obd_ioctl_freedata(buf, len);
2230         return rc;
2231 }
2232
2233 int ll_process_config(struct lustre_cfg *lcfg)
2234 {
2235         char *ptr;
2236         void *sb;
2237         struct lprocfs_static_vars lvars;
2238         unsigned long x;
2239         int rc = 0;
2240
2241         lprocfs_llite_init_vars(&lvars);
2242
2243         /* The instance name contains the sb: lustre-client-aacfe000 */
2244         ptr = strrchr(lustre_cfg_string(lcfg, 0), '-');
2245         if (!ptr || !*(++ptr))
2246                 return -EINVAL;
2247         rc = kstrtoul(ptr, 16, &x);
2248         if (rc != 0)
2249                 return -EINVAL;
2250         sb = (void *)x;
2251         /* This better be a real Lustre superblock! */
2252         LASSERT(s2lsi((struct super_block *)sb)->lsi_lmd->lmd_magic == LMD_MAGIC);
2253
2254         /* Note we have not called client_common_fill_super yet, so
2255          * proc fns must be able to handle that!
2256          */
2257         rc = class_process_proc_param(PARAM_LLITE, lvars.obd_vars,
2258                                       lcfg, sb);
2259         if (rc > 0)
2260                 rc = 0;
2261         return rc;
2262 }
2263
2264 /* this function prepares md_op_data hint for passing ot down to MD stack. */
2265 struct md_op_data *ll_prep_md_op_data(struct md_op_data *op_data,
2266                                       struct inode *i1, struct inode *i2,
2267                                       const char *name, size_t namelen,
2268                                       u32 mode, __u32 opc, void *data)
2269 {
2270         if (!name) {
2271                 /* Do not reuse namelen for something else. */
2272                 if (namelen)
2273                         return ERR_PTR(-EINVAL);
2274         } else {
2275                 if (namelen > ll_i2sbi(i1)->ll_namelen)
2276                         return ERR_PTR(-ENAMETOOLONG);
2277
2278                 if (!lu_name_is_valid_2(name, namelen))
2279                         return ERR_PTR(-EINVAL);
2280         }
2281
2282         if (!op_data)
2283                 op_data = kzalloc(sizeof(*op_data), GFP_NOFS);
2284
2285         if (!op_data)
2286                 return ERR_PTR(-ENOMEM);
2287
2288         ll_i2gids(op_data->op_suppgids, i1, i2);
2289         op_data->op_fid1 = *ll_inode2fid(i1);
2290         op_data->op_default_stripe_offset = -1;
2291         if (S_ISDIR(i1->i_mode)) {
2292                 op_data->op_mea1 = ll_i2info(i1)->lli_lsm_md;
2293                 if (opc == LUSTRE_OPC_MKDIR)
2294                         op_data->op_default_stripe_offset =
2295                                 ll_i2info(i1)->lli_def_stripe_offset;
2296         }
2297
2298         if (i2) {
2299                 op_data->op_fid2 = *ll_inode2fid(i2);
2300                 if (S_ISDIR(i2->i_mode))
2301                         op_data->op_mea2 = ll_i2info(i2)->lli_lsm_md;
2302         } else {
2303                 fid_zero(&op_data->op_fid2);
2304         }
2305
2306         if (ll_i2sbi(i1)->ll_flags & LL_SBI_64BIT_HASH)
2307                 op_data->op_cli_flags |= CLI_HASH64;
2308
2309         if (ll_need_32bit_api(ll_i2sbi(i1)))
2310                 op_data->op_cli_flags |= CLI_API32;
2311
2312         op_data->op_name = name;
2313         op_data->op_namelen = namelen;
2314         op_data->op_mode = mode;
2315         op_data->op_mod_time = ktime_get_real_seconds();
2316         op_data->op_fsuid = from_kuid(&init_user_ns, current_fsuid());
2317         op_data->op_fsgid = from_kgid(&init_user_ns, current_fsgid());
2318         op_data->op_cap = cfs_curproc_cap_pack();
2319         if ((opc == LUSTRE_OPC_CREATE) && name &&
2320             filename_is_volatile(name, namelen, &op_data->op_mds))
2321                 op_data->op_bias |= MDS_CREATE_VOLATILE;
2322         else
2323                 op_data->op_mds = 0;
2324         op_data->op_data = data;
2325
2326         return op_data;
2327 }
2328
2329 void ll_finish_md_op_data(struct md_op_data *op_data)
2330 {
2331         kfree(op_data);
2332 }
2333
2334 int ll_show_options(struct seq_file *seq, struct dentry *dentry)
2335 {
2336         struct ll_sb_info *sbi;
2337
2338         LASSERT(seq && dentry);
2339         sbi = ll_s2sbi(dentry->d_sb);
2340
2341         if (sbi->ll_flags & LL_SBI_NOLCK)
2342                 seq_puts(seq, ",nolock");
2343
2344         if (sbi->ll_flags & LL_SBI_FLOCK)
2345                 seq_puts(seq, ",flock");
2346
2347         if (sbi->ll_flags & LL_SBI_LOCALFLOCK)
2348                 seq_puts(seq, ",localflock");
2349
2350         if (sbi->ll_flags & LL_SBI_USER_XATTR)
2351                 seq_puts(seq, ",user_xattr");
2352
2353         if (sbi->ll_flags & LL_SBI_LAZYSTATFS)
2354                 seq_puts(seq, ",lazystatfs");
2355
2356         if (sbi->ll_flags & LL_SBI_USER_FID2PATH)
2357                 seq_puts(seq, ",user_fid2path");
2358
2359         if (sbi->ll_flags & LL_SBI_ALWAYS_PING)
2360                 seq_puts(seq, ",always_ping");
2361
2362         return 0;
2363 }
2364
2365 /**
2366  * Get obd name by cmd, and copy out to user space
2367  */
2368 int ll_get_obd_name(struct inode *inode, unsigned int cmd, unsigned long arg)
2369 {
2370         struct ll_sb_info *sbi = ll_i2sbi(inode);
2371         struct obd_device *obd;
2372
2373         if (cmd == OBD_IOC_GETDTNAME)
2374                 obd = class_exp2obd(sbi->ll_dt_exp);
2375         else if (cmd == OBD_IOC_GETMDNAME)
2376                 obd = class_exp2obd(sbi->ll_md_exp);
2377         else
2378                 return -EINVAL;
2379
2380         if (!obd)
2381                 return -ENOENT;
2382
2383         if (copy_to_user((void __user *)arg, obd->obd_name,
2384                          strlen(obd->obd_name) + 1))
2385                 return -EFAULT;
2386
2387         return 0;
2388 }
2389
2390 /**
2391  * Get lustre file system name by \a sbi. If \a buf is provided(non-NULL), the
2392  * fsname will be returned in this buffer; otherwise, a static buffer will be
2393  * used to store the fsname and returned to caller.
2394  */
2395 char *ll_get_fsname(struct super_block *sb, char *buf, int buflen)
2396 {
2397         static char fsname_static[MTI_NAME_MAXLEN];
2398         struct lustre_sb_info *lsi = s2lsi(sb);
2399         char *ptr;
2400         int len;
2401
2402         if (!buf) {
2403                 /* this means the caller wants to use static buffer
2404                  * and it doesn't care about race. Usually this is
2405                  * in error reporting path
2406                  */
2407                 buf = fsname_static;
2408                 buflen = sizeof(fsname_static);
2409         }
2410
2411         len = strlen(lsi->lsi_lmd->lmd_profile);
2412         ptr = strrchr(lsi->lsi_lmd->lmd_profile, '-');
2413         if (ptr && (strcmp(ptr, "-client") == 0))
2414                 len -= 7;
2415
2416         if (unlikely(len >= buflen))
2417                 len = buflen - 1;
2418         strncpy(buf, lsi->lsi_lmd->lmd_profile, len);
2419         buf[len] = '\0';
2420
2421         return buf;
2422 }
2423
2424 void ll_dirty_page_discard_warn(struct page *page, int ioret)
2425 {
2426         char *buf, *path = NULL;
2427         struct dentry *dentry = NULL;
2428         struct vvp_object *obj = cl_inode2vvp(page->mapping->host);
2429
2430         /* this can be called inside spin lock so use GFP_ATOMIC. */
2431         buf = (char *)__get_free_page(GFP_ATOMIC);
2432         if (buf) {
2433                 dentry = d_find_alias(page->mapping->host);
2434                 if (dentry)
2435                         path = dentry_path_raw(dentry, buf, PAGE_SIZE);
2436         }
2437
2438         CDEBUG(D_WARNING,
2439                "%s: dirty page discard: %s/fid: " DFID "/%s may get corrupted (rc %d)\n",
2440                ll_get_fsname(page->mapping->host->i_sb, NULL, 0),
2441                s2lsi(page->mapping->host->i_sb)->lsi_lmd->lmd_dev,
2442                PFID(&obj->vob_header.coh_lu.loh_fid),
2443                (path && !IS_ERR(path)) ? path : "", ioret);
2444
2445         if (dentry)
2446                 dput(dentry);
2447
2448         if (buf)
2449                 free_page((unsigned long)buf);
2450 }
2451
2452 ssize_t ll_copy_user_md(const struct lov_user_md __user *md,
2453                         struct lov_user_md **kbuf)
2454 {
2455         struct lov_user_md lum;
2456         ssize_t lum_size;
2457
2458         if (copy_from_user(&lum, md, sizeof(lum))) {
2459                 lum_size = -EFAULT;
2460                 goto no_kbuf;
2461         }
2462
2463         lum_size = ll_lov_user_md_size(&lum);
2464         if (lum_size < 0)
2465                 goto no_kbuf;
2466
2467         *kbuf = kzalloc(lum_size, GFP_NOFS);
2468         if (!*kbuf) {
2469                 lum_size = -ENOMEM;
2470                 goto no_kbuf;
2471         }
2472
2473         if (copy_from_user(*kbuf, md, lum_size) != 0) {
2474                 kfree(*kbuf);
2475                 *kbuf = NULL;
2476                 lum_size = -EFAULT;
2477         }
2478 no_kbuf:
2479         return lum_size;
2480 }
2481
2482 /*
2483  * Compute llite root squash state after a change of root squash
2484  * configuration setting or add/remove of a lnet nid
2485  */
2486 void ll_compute_rootsquash_state(struct ll_sb_info *sbi)
2487 {
2488         struct root_squash_info *squash = &sbi->ll_squash;
2489         lnet_process_id_t id;
2490         bool matched;
2491         int i;
2492
2493         /* Update norootsquash flag */
2494         down_write(&squash->rsi_sem);
2495         if (list_empty(&squash->rsi_nosquash_nids)) {
2496                 sbi->ll_flags &= ~LL_SBI_NOROOTSQUASH;
2497         } else {
2498                 /*
2499                  * Do not apply root squash as soon as one of our NIDs is
2500                  * in the nosquash_nids list
2501                  */
2502                 matched = false;
2503                 i = 0;
2504
2505                 while (LNetGetId(i++, &id) != -ENOENT) {
2506                         if (LNET_NETTYP(LNET_NIDNET(id.nid)) == LOLND)
2507                                 continue;
2508                         if (cfs_match_nid(id.nid, &squash->rsi_nosquash_nids)) {
2509                                 matched = true;
2510                                 break;
2511                         }
2512                 }
2513                 if (matched)
2514                         sbi->ll_flags |= LL_SBI_NOROOTSQUASH;
2515                 else
2516                         sbi->ll_flags &= ~LL_SBI_NOROOTSQUASH;
2517         }
2518         up_write(&squash->rsi_sem);
2519 }
2520
2521 /**
2522  * Parse linkea content to extract information about a given hardlink
2523  *
2524  * \param[in]   ldata           - Initialized linkea data
2525  * \param[in]   linkno          - Link identifier
2526  * \param[out]  parent_fid      - The entry's parent FID
2527  * \param[in]   size            - Entry name destination buffer
2528  *
2529  * \retval 0 on success
2530  * \retval Appropriate negative error code on failure
2531  */
2532 static int ll_linkea_decode(struct linkea_data *ldata, unsigned int linkno,
2533                             struct lu_fid *parent_fid, struct lu_name *ln)
2534 {
2535         unsigned int idx;
2536         int rc;
2537
2538         rc = linkea_init(ldata);
2539         if (rc < 0)
2540                 return rc;
2541
2542         if (linkno >= ldata->ld_leh->leh_reccount)
2543                 /* beyond last link */
2544                 return -ENODATA;
2545
2546         linkea_first_entry(ldata);
2547         for (idx = 0; ldata->ld_lee; idx++) {
2548                 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen, ln,
2549                                     parent_fid);
2550                 if (idx == linkno)
2551                         break;
2552
2553                 linkea_next_entry(ldata);
2554         }
2555
2556         if (idx < linkno)
2557                 return -ENODATA;
2558
2559         return 0;
2560 }
2561
2562 /**
2563  * Get parent FID and name of an identified link. Operation is performed for
2564  * a given link number, letting the caller iterate over linkno to list one or
2565  * all links of an entry.
2566  *
2567  * \param[in]     file  - File descriptor against which to perform the operation
2568  * \param[in,out] arg   - User-filled structure containing the linkno to operate
2569  *                        on and the available size. It is eventually filled with
2570  *                        the requested information or left untouched on error
2571  *
2572  * \retval - 0 on success
2573  * \retval - Appropriate negative error code on failure
2574  */
2575 int ll_getparent(struct file *file, struct getparent __user *arg)
2576 {
2577         struct inode *inode = file_inode(file);
2578         struct linkea_data *ldata;
2579         struct lu_fid parent_fid;
2580         struct lu_buf buf = {
2581                 .lb_buf = NULL,
2582                 .lb_len = 0
2583         };
2584         struct lu_name ln;
2585         u32 name_size;
2586         u32 linkno;
2587         int rc;
2588
2589         if (!capable(CFS_CAP_DAC_READ_SEARCH) &&
2590             !(ll_i2sbi(inode)->ll_flags & LL_SBI_USER_FID2PATH))
2591                 return -EPERM;
2592
2593         if (get_user(name_size, &arg->gp_name_size))
2594                 return -EFAULT;
2595
2596         if (get_user(linkno, &arg->gp_linkno))
2597                 return -EFAULT;
2598
2599         if (name_size > PATH_MAX)
2600                 return -EINVAL;
2601
2602         ldata = kzalloc(sizeof(*ldata), GFP_NOFS);
2603         if (!ldata)
2604                 return -ENOMEM;
2605
2606         rc = linkea_data_new(ldata, &buf);
2607         if (rc < 0)
2608                 goto ldata_free;
2609
2610         rc = ll_xattr_list(inode, XATTR_NAME_LINK, XATTR_TRUSTED_T, buf.lb_buf,
2611                            buf.lb_len, OBD_MD_FLXATTR);
2612         if (rc < 0)
2613                 goto lb_free;
2614
2615         rc = ll_linkea_decode(ldata, linkno, &parent_fid, &ln);
2616         if (rc < 0)
2617                 goto lb_free;
2618
2619         if (ln.ln_namelen >= name_size) {
2620                 rc = -EOVERFLOW;
2621                 goto lb_free;
2622         }
2623
2624         if (copy_to_user(&arg->gp_fid, &parent_fid, sizeof(arg->gp_fid))) {
2625                 rc = -EFAULT;
2626                 goto lb_free;
2627         }
2628
2629         if (copy_to_user(&arg->gp_name, ln.ln_name, ln.ln_namelen)) {
2630                 rc = -EFAULT;
2631                 goto lb_free;
2632         }
2633
2634         if (put_user('\0', arg->gp_name + ln.ln_namelen)) {
2635                 rc = -EFAULT;
2636                 goto lb_free;
2637         }
2638
2639 lb_free:
2640         lu_buf_free(&buf);
2641 ldata_free:
2642         kfree(ldata);
2643         return rc;
2644 }