Whamcloud - gitweb
f3210d7caa5db2467316bf4439a8b6292def4c4f
[fs/lustre-release.git] / lustre / llite / llite_lib.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Whamcloud, Inc.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/llite/llite_lib.c
37  *
38  * Lustre Light Super operations
39  */
40
41 #define DEBUG_SUBSYSTEM S_LLITE
42
43 #include <linux/module.h>
44 #include <linux/types.h>
45 #include <linux/version.h>
46 #include <linux/mm.h>
47
48 #include <lustre_lite.h>
49 #include <lustre_ha.h>
50 #include <lustre_dlm.h>
51 #include <lprocfs_status.h>
52 #include <lustre_disk.h>
53 #include <lustre_param.h>
54 #include <lustre_log.h>
55 #include <cl_object.h>
56 #include <obd_cksum.h>
57 #include "llite_internal.h"
58
59 cfs_mem_cache_t *ll_file_data_slab;
60
61 CFS_LIST_HEAD(ll_super_blocks);
62 DEFINE_SPINLOCK(ll_sb_lock);
63
64 #ifndef MS_HAS_NEW_AOPS
65 extern struct address_space_operations ll_aops;
66 extern struct address_space_operations ll_dir_aops;
67 #else
68 extern struct address_space_operations_ext ll_aops;
69 extern struct address_space_operations_ext ll_dir_aops;
70 #endif
71
72 #ifndef log2
73 #define log2(n) cfs_ffz(~(n))
74 #endif
75
76 static struct ll_sb_info *ll_init_sbi(void)
77 {
78         struct ll_sb_info *sbi = NULL;
79         unsigned long pages;
80         struct sysinfo si;
81         class_uuid_t uuid;
82         int i;
83         ENTRY;
84
85         OBD_ALLOC(sbi, sizeof(*sbi));
86         if (!sbi)
87                 RETURN(NULL);
88
89         cfs_spin_lock_init(&sbi->ll_lock);
90         cfs_mutex_init(&sbi->ll_lco.lco_lock);
91         cfs_spin_lock_init(&sbi->ll_pp_extent_lock);
92         cfs_spin_lock_init(&sbi->ll_process_lock);
93         sbi->ll_rw_stats_on = 0;
94
95         si_meminfo(&si);
96         pages = si.totalram - si.totalhigh;
97         if (pages >> (20 - CFS_PAGE_SHIFT) < 512) {
98 #ifdef HAVE_BGL_SUPPORT
99                 sbi->ll_async_page_max = pages / 4;
100 #else
101                 sbi->ll_async_page_max = pages / 2;
102 #endif
103         } else {
104                 sbi->ll_async_page_max = (pages / 4) * 3;
105         }
106
107         sbi->ll_ra_info.ra_max_pages_per_file = min(pages / 32,
108                                            SBI_DEFAULT_READAHEAD_MAX);
109         sbi->ll_ra_info.ra_max_pages = sbi->ll_ra_info.ra_max_pages_per_file;
110         sbi->ll_ra_info.ra_max_read_ahead_whole_pages =
111                                            SBI_DEFAULT_READAHEAD_WHOLE_MAX;
112         CFS_INIT_LIST_HEAD(&sbi->ll_conn_chain);
113         CFS_INIT_LIST_HEAD(&sbi->ll_orphan_dentry_list);
114
115         ll_generate_random_uuid(uuid);
116         class_uuid_unparse(uuid, &sbi->ll_sb_uuid);
117         CDEBUG(D_CONFIG, "generated uuid: %s\n", sbi->ll_sb_uuid.uuid);
118
119         cfs_spin_lock(&ll_sb_lock);
120         cfs_list_add_tail(&sbi->ll_list, &ll_super_blocks);
121         cfs_spin_unlock(&ll_sb_lock);
122
123         sbi->ll_flags |= LL_SBI_VERBOSE;
124 #ifdef ENABLE_CHECKSUM
125         sbi->ll_flags |= LL_SBI_CHECKSUM;
126 #endif
127
128 #ifdef HAVE_LRU_RESIZE_SUPPORT
129         sbi->ll_flags |= LL_SBI_LRU_RESIZE;
130 #endif
131
132         for (i = 0; i <= LL_PROCESS_HIST_MAX; i++) {
133                 cfs_spin_lock_init(&sbi->ll_rw_extents_info.pp_extents[i]. \
134                                    pp_r_hist.oh_lock);
135                 cfs_spin_lock_init(&sbi->ll_rw_extents_info.pp_extents[i]. \
136                                    pp_w_hist.oh_lock);
137         }
138
139         /* metadata statahead is enabled by default */
140         sbi->ll_sa_max = LL_SA_RPC_DEF;
141         cfs_atomic_set(&sbi->ll_sa_total, 0);
142         cfs_atomic_set(&sbi->ll_sa_wrong, 0);
143         cfs_atomic_set(&sbi->ll_agl_total, 0);
144         sbi->ll_flags |= LL_SBI_AGL_ENABLED;
145
146         RETURN(sbi);
147 }
148
149 void ll_free_sbi(struct super_block *sb)
150 {
151         struct ll_sb_info *sbi = ll_s2sbi(sb);
152         ENTRY;
153
154         if (sbi != NULL) {
155                 cfs_spin_lock(&ll_sb_lock);
156                 cfs_list_del(&sbi->ll_list);
157                 cfs_spin_unlock(&ll_sb_lock);
158                 OBD_FREE(sbi, sizeof(*sbi));
159         }
160         EXIT;
161 }
162
163 static struct dentry_operations ll_d_root_ops = {
164         .d_compare = ll_dcompare,
165         .d_revalidate = ll_revalidate_nd,
166 };
167
168 static int client_common_fill_super(struct super_block *sb, char *md, char *dt,
169                                     struct vfsmount *mnt)
170 {
171         struct inode *root = 0;
172         struct ll_sb_info *sbi = ll_s2sbi(sb);
173         struct obd_device *obd;
174         struct obd_capa *oc = NULL;
175         struct obd_statfs *osfs = NULL;
176         struct ptlrpc_request *request = NULL;
177         struct obd_connect_data *data = NULL;
178         struct obd_uuid *uuid;
179         struct md_op_data *op_data;
180         struct lustre_md lmd;
181         obd_valid valid;
182         int size, err, checksum;
183         ENTRY;
184
185         obd = class_name2obd(md);
186         if (!obd) {
187                 CERROR("MD %s: not setup or attached\n", md);
188                 RETURN(-EINVAL);
189         }
190
191         OBD_ALLOC_PTR(data);
192         if (data == NULL)
193                 RETURN(-ENOMEM);
194
195         OBD_ALLOC_PTR(osfs);
196         if (osfs == NULL) {
197                 OBD_FREE_PTR(data);
198                 RETURN(-ENOMEM);
199         }
200
201         if (proc_lustre_fs_root) {
202                 err = lprocfs_register_mountpoint(proc_lustre_fs_root, sb,
203                                                   dt, md);
204                 if (err < 0)
205                         CERROR("could not register mount in /proc/fs/lustre\n");
206         }
207
208         /* indicate the features supported by this client */
209         data->ocd_connect_flags = OBD_CONNECT_IBITS    | OBD_CONNECT_NODEVOH  |
210                                   OBD_CONNECT_ATTRFID  |
211                                   OBD_CONNECT_VERSION  | OBD_CONNECT_BRW_SIZE |
212                                   OBD_CONNECT_MDS_CAPA | OBD_CONNECT_OSS_CAPA |
213                                   OBD_CONNECT_CANCELSET | OBD_CONNECT_FID     |
214                                   OBD_CONNECT_AT       | OBD_CONNECT_LOV_V3   |
215                                   OBD_CONNECT_RMT_CLIENT | OBD_CONNECT_VBR    |
216                                   OBD_CONNECT_FULL20   | OBD_CONNECT_64BITHASH|
217                                   OBD_CONNECT_EINPROGRESS |
218                                   OBD_CONNECT_JOBSTATS;
219
220         if (sbi->ll_flags & LL_SBI_SOM_PREVIEW)
221                 data->ocd_connect_flags |= OBD_CONNECT_SOM;
222
223 #ifdef HAVE_LRU_RESIZE_SUPPORT
224         if (sbi->ll_flags & LL_SBI_LRU_RESIZE)
225                 data->ocd_connect_flags |= OBD_CONNECT_LRU_RESIZE;
226 #endif
227 #ifdef CONFIG_FS_POSIX_ACL
228         data->ocd_connect_flags |= OBD_CONNECT_ACL;
229 #endif
230
231         if (OBD_FAIL_CHECK(OBD_FAIL_MDC_LIGHTWEIGHT))
232                 /* flag mdc connection as lightweight, only used for test
233                  * purpose, use with care */
234                 data->ocd_connect_flags |= OBD_CONNECT_LIGHTWEIGHT;
235
236         data->ocd_ibits_known = MDS_INODELOCK_FULL;
237         data->ocd_version = LUSTRE_VERSION_CODE;
238
239         if (sb->s_flags & MS_RDONLY)
240                 data->ocd_connect_flags |= OBD_CONNECT_RDONLY;
241         if (sbi->ll_flags & LL_SBI_USER_XATTR)
242                 data->ocd_connect_flags |= OBD_CONNECT_XATTR;
243
244 #ifdef HAVE_MS_FLOCK_LOCK
245         /* force vfs to use lustre handler for flock() calls - bug 10743 */
246         sb->s_flags |= MS_FLOCK_LOCK;
247 #endif
248 #ifdef MS_HAS_NEW_AOPS
249         sb->s_flags |= MS_HAS_NEW_AOPS;
250 #endif
251
252         if (sbi->ll_flags & LL_SBI_FLOCK)
253                 sbi->ll_fop = &ll_file_operations_flock;
254         else if (sbi->ll_flags & LL_SBI_LOCALFLOCK)
255                 sbi->ll_fop = &ll_file_operations;
256         else
257                 sbi->ll_fop = &ll_file_operations_noflock;
258
259         /* real client */
260         data->ocd_connect_flags |= OBD_CONNECT_REAL;
261         if (sbi->ll_flags & LL_SBI_RMT_CLIENT)
262                 data->ocd_connect_flags |= OBD_CONNECT_RMT_CLIENT_FORCE;
263
264         data->ocd_brw_size = PTLRPC_MAX_BRW_SIZE;
265
266         err = obd_connect(NULL, &sbi->ll_md_exp, obd, &sbi->ll_sb_uuid, data, NULL);
267         if (err == -EBUSY) {
268                 LCONSOLE_ERROR_MSG(0x14f, "An MDT (md %s) is performing "
269                                    "recovery, of which this client is not a "
270                                    "part. Please wait for recovery to complete,"
271                                    " abort, or time out.\n", md);
272                 GOTO(out, err);
273         } else if (err) {
274                 CERROR("cannot connect to %s: rc = %d\n", md, err);
275                 GOTO(out, err);
276         }
277
278         err = obd_fid_init(sbi->ll_md_exp);
279         if (err) {
280                 CERROR("Can't init metadata layer FID infrastructure, "
281                        "rc %d\n", err);
282                 GOTO(out_md, err);
283         }
284
285         err = obd_statfs(NULL, sbi->ll_md_exp, osfs,
286                          cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS), 0);
287         if (err)
288                 GOTO(out_md_fid, err);
289
290         /* This needs to be after statfs to ensure connect has finished.
291          * Note that "data" does NOT contain the valid connect reply.
292          * If connecting to a 1.8 server there will be no LMV device, so
293          * we can access the MDC export directly and exp_connect_flags will
294          * be non-zero, but if accessing an upgraded 2.1 server it will
295          * have the correct flags filled in.
296          * XXX: fill in the LMV exp_connect_flags from MDC(s). */
297         valid = sbi->ll_md_exp->exp_connect_flags & CLIENT_CONNECT_MDT_REQD;
298         if (sbi->ll_md_exp->exp_connect_flags != 0 &&
299             valid != CLIENT_CONNECT_MDT_REQD) {
300                 char *buf;
301
302                 OBD_ALLOC_WAIT(buf, CFS_PAGE_SIZE);
303                 obd_connect_flags2str(buf, CFS_PAGE_SIZE,
304                                       valid ^ CLIENT_CONNECT_MDT_REQD, ",");
305                 LCONSOLE_ERROR_MSG(0x170, "Server %s does not support "
306                                    "feature(s) needed for correct operation "
307                                    "of this client (%s). Please upgrade "
308                                    "server or downgrade client.\n",
309                                    sbi->ll_md_exp->exp_obd->obd_name, buf);
310                 OBD_FREE(buf, CFS_PAGE_SIZE);
311                 GOTO(out_md, err = -EPROTO);
312         }
313
314         size = sizeof(*data);
315         err = obd_get_info(NULL, sbi->ll_md_exp, sizeof(KEY_CONN_DATA),
316                            KEY_CONN_DATA,  &size, data, NULL);
317         if (err) {
318                 CERROR("Get connect data failed: %d \n", err);
319                 GOTO(out_md, err);
320         }
321
322         LASSERT(osfs->os_bsize);
323         sb->s_blocksize = osfs->os_bsize;
324         sb->s_blocksize_bits = log2(osfs->os_bsize);
325         sb->s_magic = LL_SUPER_MAGIC;
326         sb->s_maxbytes = MAX_LFS_FILESIZE;
327         sbi->ll_namelen = osfs->os_namelen;
328         sbi->ll_max_rw_chunk = LL_DEFAULT_MAX_RW_CHUNK;
329
330         if ((sbi->ll_flags & LL_SBI_USER_XATTR) &&
331             !(data->ocd_connect_flags & OBD_CONNECT_XATTR)) {
332                 LCONSOLE_INFO("Disabling user_xattr feature because "
333                               "it is not supported on the server\n");
334                 sbi->ll_flags &= ~LL_SBI_USER_XATTR;
335         }
336
337         if (data->ocd_connect_flags & OBD_CONNECT_ACL) {
338 #ifdef MS_POSIXACL
339                 sb->s_flags |= MS_POSIXACL;
340 #endif
341                 sbi->ll_flags |= LL_SBI_ACL;
342         } else {
343                 LCONSOLE_INFO("client wants to enable acl, but mdt not!\n");
344 #ifdef MS_POSIXACL
345                 sb->s_flags &= ~MS_POSIXACL;
346 #endif
347                 sbi->ll_flags &= ~LL_SBI_ACL;
348         }
349
350         if (data->ocd_connect_flags & OBD_CONNECT_RMT_CLIENT) {
351                 if (!(sbi->ll_flags & LL_SBI_RMT_CLIENT)) {
352                         sbi->ll_flags |= LL_SBI_RMT_CLIENT;
353                         LCONSOLE_INFO("client is set as remote by default.\n");
354                 }
355         } else {
356                 if (sbi->ll_flags & LL_SBI_RMT_CLIENT) {
357                         sbi->ll_flags &= ~LL_SBI_RMT_CLIENT;
358                         LCONSOLE_INFO("client claims to be remote, but server "
359                                       "rejected, forced to be local.\n");
360                 }
361         }
362
363         if (data->ocd_connect_flags & OBD_CONNECT_MDS_CAPA) {
364                 LCONSOLE_INFO("client enabled MDS capability!\n");
365                 sbi->ll_flags |= LL_SBI_MDS_CAPA;
366         }
367
368         if (data->ocd_connect_flags & OBD_CONNECT_OSS_CAPA) {
369                 LCONSOLE_INFO("client enabled OSS capability!\n");
370                 sbi->ll_flags |= LL_SBI_OSS_CAPA;
371         }
372
373         if (data->ocd_connect_flags & OBD_CONNECT_64BITHASH)
374                 sbi->ll_flags |= LL_SBI_64BIT_HASH;
375
376         if (data->ocd_connect_flags & OBD_CONNECT_BRW_SIZE)
377                 sbi->ll_md_brw_size = data->ocd_brw_size;
378         else
379                 sbi->ll_md_brw_size = CFS_PAGE_SIZE;
380
381         if (data->ocd_connect_flags & OBD_CONNECT_LAYOUTLOCK) {
382                 LCONSOLE_INFO("Layout lock feature supported.\n");
383                 sbi->ll_flags |= LL_SBI_LAYOUT_LOCK;
384         }
385
386         obd = class_name2obd(dt);
387         if (!obd) {
388                 CERROR("DT %s: not setup or attached\n", dt);
389                 GOTO(out_md_fid, err = -ENODEV);
390         }
391
392         data->ocd_connect_flags = OBD_CONNECT_GRANT     | OBD_CONNECT_VERSION  |
393                                   OBD_CONNECT_REQPORTAL | OBD_CONNECT_BRW_SIZE |
394                                   OBD_CONNECT_CANCELSET | OBD_CONNECT_FID      |
395                                   OBD_CONNECT_SRVLOCK   | OBD_CONNECT_TRUNCLOCK|
396                                   OBD_CONNECT_AT | OBD_CONNECT_RMT_CLIENT |
397                                   OBD_CONNECT_OSS_CAPA | OBD_CONNECT_VBR|
398                                   OBD_CONNECT_FULL20 | OBD_CONNECT_64BITHASH |
399                                   OBD_CONNECT_MAXBYTES |
400                                   OBD_CONNECT_EINPROGRESS |
401                                   OBD_CONNECT_JOBSTATS;
402
403         if (sbi->ll_flags & LL_SBI_SOM_PREVIEW)
404                 data->ocd_connect_flags |= OBD_CONNECT_SOM;
405
406         if (!OBD_FAIL_CHECK(OBD_FAIL_OSC_CONNECT_CKSUM)) {
407                 /* OBD_CONNECT_CKSUM should always be set, even if checksums are
408                  * disabled by default, because it can still be enabled on the
409                  * fly via /proc. As a consequence, we still need to come to an
410                  * agreement on the supported algorithms at connect time */
411                 data->ocd_connect_flags |= OBD_CONNECT_CKSUM;
412
413                 if (OBD_FAIL_CHECK(OBD_FAIL_OSC_CKSUM_ADLER_ONLY))
414                         data->ocd_cksum_types = OBD_CKSUM_ADLER;
415                 else
416                         data->ocd_cksum_types = cksum_types_supported_client();
417         }
418
419 #ifdef HAVE_LRU_RESIZE_SUPPORT
420         data->ocd_connect_flags |= OBD_CONNECT_LRU_RESIZE;
421 #endif
422         if (sbi->ll_flags & LL_SBI_RMT_CLIENT)
423                 data->ocd_connect_flags |= OBD_CONNECT_RMT_CLIENT_FORCE;
424
425         CDEBUG(D_RPCTRACE, "ocd_connect_flags: "LPX64" ocd_version: %d "
426                "ocd_grant: %d\n", data->ocd_connect_flags,
427                data->ocd_version, data->ocd_grant);
428
429         obd->obd_upcall.onu_owner = &sbi->ll_lco;
430         obd->obd_upcall.onu_upcall = cl_ocd_update;
431
432         data->ocd_brw_size = PTLRPC_MAX_BRW_SIZE;
433
434         err = obd_connect(NULL, &sbi->ll_dt_exp, obd, &sbi->ll_sb_uuid, data, NULL);
435         if (err == -EBUSY) {
436                 LCONSOLE_ERROR_MSG(0x150, "An OST (dt %s) is performing "
437                                    "recovery, of which this client is not a "
438                                    "part.  Please wait for recovery to "
439                                    "complete, abort, or time out.\n", dt);
440                 GOTO(out_md_fid, err);
441         } else if (err) {
442                 CERROR("Cannot connect to %s: rc = %d\n", dt, err);
443                 GOTO(out_md_fid, err);
444         }
445
446         err = obd_fid_init(sbi->ll_dt_exp);
447         if (err) {
448                 CERROR("Can't init data layer FID infrastructure, "
449                        "rc %d\n", err);
450                 GOTO(out_dt, err);
451         }
452
453         cfs_mutex_lock(&sbi->ll_lco.lco_lock);
454         sbi->ll_lco.lco_flags = data->ocd_connect_flags;
455         sbi->ll_lco.lco_md_exp = sbi->ll_md_exp;
456         sbi->ll_lco.lco_dt_exp = sbi->ll_dt_exp;
457         cfs_mutex_unlock(&sbi->ll_lco.lco_lock);
458
459         fid_zero(&sbi->ll_root_fid);
460         err = md_getstatus(sbi->ll_md_exp, &sbi->ll_root_fid, &oc);
461         if (err) {
462                 CERROR("cannot mds_connect: rc = %d\n", err);
463                 GOTO(out_lock_cn_cb, err);
464         }
465         if (!fid_is_sane(&sbi->ll_root_fid)) {
466                 CERROR("Invalid root fid during mount\n");
467                 GOTO(out_lock_cn_cb, err = -EINVAL);
468         }
469         CDEBUG(D_SUPER, "rootfid "DFID"\n", PFID(&sbi->ll_root_fid));
470
471         sb->s_op = &lustre_super_operations;
472 #if THREAD_SIZE >= 8192 /*b=17630*/ && !defined(HAVE_FSTYPE_MOUNT) /*LU-812*/
473         sb->s_export_op = &lustre_export_operations;
474 #endif
475
476         /* make root inode
477          * XXX: move this to after cbd setup? */
478         valid = OBD_MD_FLGETATTR | OBD_MD_FLBLOCKS | OBD_MD_FLMDSCAPA;
479         if (sbi->ll_flags & LL_SBI_RMT_CLIENT)
480                 valid |= OBD_MD_FLRMTPERM;
481         else if (sbi->ll_flags & LL_SBI_ACL)
482                 valid |= OBD_MD_FLACL;
483
484         OBD_ALLOC_PTR(op_data);
485         if (op_data == NULL)
486                 GOTO(out_lock_cn_cb, err = -ENOMEM);
487
488         op_data->op_fid1 = sbi->ll_root_fid;
489         op_data->op_mode = 0;
490         op_data->op_capa1 = oc;
491         op_data->op_valid = valid;
492
493         err = md_getattr(sbi->ll_md_exp, op_data, &request);
494         if (oc)
495                 capa_put(oc);
496         OBD_FREE_PTR(op_data);
497         if (err) {
498                 CERROR("md_getattr failed for root: rc = %d\n", err);
499                 GOTO(out_lock_cn_cb, err);
500         }
501         err = md_get_lustre_md(sbi->ll_md_exp, request, sbi->ll_dt_exp,
502                                sbi->ll_md_exp, &lmd);
503         if (err) {
504                 CERROR("failed to understand root inode md: rc = %d\n", err);
505                 ptlrpc_req_finished (request);
506                 GOTO(out_lock_cn_cb, err);
507         }
508
509         LASSERT(fid_is_sane(&sbi->ll_root_fid));
510         root = ll_iget(sb, cl_fid_build_ino(&sbi->ll_root_fid, 0), &lmd);
511         md_free_lustre_md(sbi->ll_md_exp, &lmd);
512         ptlrpc_req_finished(request);
513
514         if (root == NULL || IS_ERR(root)) {
515                 if (lmd.lsm)
516                         obd_free_memmd(sbi->ll_dt_exp, &lmd.lsm);
517 #ifdef CONFIG_FS_POSIX_ACL
518                 if (lmd.posix_acl) {
519                         posix_acl_release(lmd.posix_acl);
520                         lmd.posix_acl = NULL;
521                 }
522 #endif
523                 err = IS_ERR(root) ? PTR_ERR(root) : -EBADF;
524                 root = NULL;
525                 CERROR("lustre_lite: bad iget4 for root\n");
526                 GOTO(out_root, err);
527         }
528
529         err = ll_close_thread_start(&sbi->ll_lcq);
530         if (err) {
531                 CERROR("cannot start close thread: rc %d\n", err);
532                 GOTO(out_root, err);
533         }
534
535 #ifdef CONFIG_FS_POSIX_ACL
536         if (sbi->ll_flags & LL_SBI_RMT_CLIENT) {
537                 rct_init(&sbi->ll_rct);
538                 et_init(&sbi->ll_et);
539         }
540 #endif
541
542         checksum = sbi->ll_flags & LL_SBI_CHECKSUM;
543         err = obd_set_info_async(NULL, sbi->ll_dt_exp, sizeof(KEY_CHECKSUM),
544                                  KEY_CHECKSUM, sizeof(checksum), &checksum,
545                                  NULL);
546         cl_sb_init(sb);
547
548         sb->s_root = d_alloc_root(root);
549 #ifdef HAVE_DCACHE_LOCK
550         sb->s_root->d_op = &ll_d_root_ops;
551 #else
552         /* kernel >= 2.6.38 store dentry operations in sb->s_d_op. */
553         d_set_d_op(sb->s_root, &ll_d_root_ops);
554         sb->s_d_op = &ll_d_ops;
555 #endif
556
557         sbi->ll_sdev_orig = sb->s_dev;
558
559         /* We set sb->s_dev equal on all lustre clients in order to support
560          * NFS export clustering.  NFSD requires that the FSID be the same
561          * on all clients. */
562         /* s_dev is also used in lt_compare() to compare two fs, but that is
563          * only a node-local comparison. */
564         uuid = obd_get_uuid(sbi->ll_md_exp);
565         if (uuid != NULL)
566                 sb->s_dev = get_uuid2int(uuid->uuid, strlen(uuid->uuid));
567         sbi->ll_mnt = mnt;
568
569         if (data != NULL)
570                 OBD_FREE_PTR(data);
571         if (osfs != NULL)
572                 OBD_FREE_PTR(osfs);
573
574         RETURN(err);
575 out_root:
576         if (root)
577                 iput(root);
578 out_lock_cn_cb:
579         obd_fid_fini(sbi->ll_dt_exp);
580 out_dt:
581         obd_disconnect(sbi->ll_dt_exp);
582         sbi->ll_dt_exp = NULL;
583 out_md_fid:
584         obd_fid_fini(sbi->ll_md_exp);
585 out_md:
586         obd_disconnect(sbi->ll_md_exp);
587         sbi->ll_md_exp = NULL;
588 out:
589         if (data != NULL)
590                 OBD_FREE_PTR(data);
591         if (osfs != NULL)
592                 OBD_FREE_PTR(osfs);
593         lprocfs_unregister_mountpoint(sbi);
594         return err;
595 }
596
597 int ll_get_max_mdsize(struct ll_sb_info *sbi, int *lmmsize)
598 {
599         int size, rc;
600
601         *lmmsize = obd_size_diskmd(sbi->ll_dt_exp, NULL);
602         size = sizeof(int);
603         rc = obd_get_info(NULL, sbi->ll_md_exp, sizeof(KEY_MAX_EASIZE),
604                           KEY_MAX_EASIZE, &size, lmmsize, NULL);
605         if (rc)
606                 CERROR("Get max mdsize error rc %d \n", rc);
607
608         RETURN(rc);
609 }
610
611 void ll_dump_inode(struct inode *inode)
612 {
613         struct list_head *tmp;
614         int dentry_count = 0;
615
616         LASSERT(inode != NULL);
617
618         list_for_each(tmp, &inode->i_dentry)
619                 dentry_count++;
620
621         CERROR("inode %p dump: dev=%s ino=%lu mode=%o count=%u, %d dentries\n",
622                inode, ll_i2mdexp(inode)->exp_obd->obd_name, inode->i_ino,
623                inode->i_mode, atomic_read(&inode->i_count), dentry_count);
624 }
625
626 void lustre_dump_dentry(struct dentry *dentry, int recur)
627 {
628         struct list_head *tmp;
629         int subdirs = 0;
630
631         LASSERT(dentry != NULL);
632
633         list_for_each(tmp, &dentry->d_subdirs)
634                 subdirs++;
635
636         CERROR("dentry %p dump: name=%.*s parent=%.*s (%p), inode=%p, count=%u,"
637                " flags=0x%x, fsdata=%p, %d subdirs\n", dentry,
638                dentry->d_name.len, dentry->d_name.name,
639                dentry->d_parent->d_name.len, dentry->d_parent->d_name.name,
640                dentry->d_parent, dentry->d_inode, d_refcount(dentry),
641                dentry->d_flags, dentry->d_fsdata, subdirs);
642         if (dentry->d_inode != NULL)
643                 ll_dump_inode(dentry->d_inode);
644
645         if (recur == 0)
646                 return;
647
648         list_for_each(tmp, &dentry->d_subdirs) {
649                 struct dentry *d = list_entry(tmp, struct dentry, d_u.d_child);
650                 lustre_dump_dentry(d, recur - 1);
651         }
652 }
653
654 void client_common_put_super(struct super_block *sb)
655 {
656         struct ll_sb_info *sbi = ll_s2sbi(sb);
657         ENTRY;
658
659 #ifdef CONFIG_FS_POSIX_ACL
660         if (sbi->ll_flags & LL_SBI_RMT_CLIENT) {
661                 et_fini(&sbi->ll_et);
662                 rct_fini(&sbi->ll_rct);
663         }
664 #endif
665
666         obd_cancel_unused(sbi->ll_dt_exp, NULL, 0, NULL);
667
668         ll_close_thread_shutdown(sbi->ll_lcq);
669
670         cl_sb_fini(sb);
671
672         cfs_list_del(&sbi->ll_conn_chain);
673
674         obd_fid_fini(sbi->ll_dt_exp);
675         obd_disconnect(sbi->ll_dt_exp);
676         sbi->ll_dt_exp = NULL;
677
678         lprocfs_unregister_mountpoint(sbi);
679
680         obd_fid_fini(sbi->ll_md_exp);
681         obd_disconnect(sbi->ll_md_exp);
682         sbi->ll_md_exp = NULL;
683
684         EXIT;
685 }
686
687 void ll_kill_super(struct super_block *sb)
688 {
689         struct ll_sb_info *sbi;
690
691         ENTRY;
692
693         /* not init sb ?*/
694         if (!(sb->s_flags & MS_ACTIVE))
695                 return;
696
697         sbi = ll_s2sbi(sb);
698         /* we need restore s_dev from changed for clustred NFS before put_super
699          * because new kernels have cached s_dev and change sb->s_dev in
700          * put_super not affected real removing devices */
701         if (sbi)
702                 sb->s_dev = sbi->ll_sdev_orig;
703         EXIT;
704 }
705
706 char *ll_read_opt(const char *opt, char *data)
707 {
708         char *value;
709         char *retval;
710         ENTRY;
711
712         CDEBUG(D_SUPER, "option: %s, data %s\n", opt, data);
713         if (strncmp(opt, data, strlen(opt)))
714                 RETURN(NULL);
715         if ((value = strchr(data, '=')) == NULL)
716                 RETURN(NULL);
717
718         value++;
719         OBD_ALLOC(retval, strlen(value) + 1);
720         if (!retval) {
721                 CERROR("out of memory!\n");
722                 RETURN(NULL);
723         }
724
725         memcpy(retval, value, strlen(value)+1);
726         CDEBUG(D_SUPER, "Assigned option: %s, value %s\n", opt, retval);
727         RETURN(retval);
728 }
729
730 static inline int ll_set_opt(const char *opt, char *data, int fl)
731 {
732         if (strncmp(opt, data, strlen(opt)) != 0)
733                 return(0);
734         else
735                 return(fl);
736 }
737
738 /* non-client-specific mount options are parsed in lmd_parse */
739 static int ll_options(char *options, int *flags)
740 {
741         int tmp;
742         char *s1 = options, *s2;
743         ENTRY;
744
745         if (!options)
746                 RETURN(0);
747
748         CDEBUG(D_CONFIG, "Parsing opts %s\n", options);
749
750         while (*s1) {
751                 CDEBUG(D_SUPER, "next opt=%s\n", s1);
752                 tmp = ll_set_opt("nolock", s1, LL_SBI_NOLCK);
753                 if (tmp) {
754                         *flags |= tmp;
755                         goto next;
756                 }
757                 tmp = ll_set_opt("flock", s1, LL_SBI_FLOCK);
758                 if (tmp) {
759                         *flags |= tmp;
760                         goto next;
761                 }
762                 tmp = ll_set_opt("localflock", s1, LL_SBI_LOCALFLOCK);
763                 if (tmp) {
764                         *flags |= tmp;
765                         goto next;
766                 }
767                 tmp = ll_set_opt("noflock", s1, LL_SBI_FLOCK|LL_SBI_LOCALFLOCK);
768                 if (tmp) {
769                         *flags &= ~tmp;
770                         goto next;
771                 }
772                 tmp = ll_set_opt("user_xattr", s1, LL_SBI_USER_XATTR);
773                 if (tmp) {
774                         *flags |= tmp;
775                         goto next;
776                 }
777                 tmp = ll_set_opt("nouser_xattr", s1, LL_SBI_USER_XATTR);
778                 if (tmp) {
779                         *flags &= ~tmp;
780                         goto next;
781                 }
782 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 5, 50, 0)
783                 tmp = ll_set_opt("acl", s1, LL_SBI_ACL);
784                 if (tmp) {
785                         /* Ignore deprecated mount option.  The client will
786                          * always try to mount with ACL support, whether this
787                          * is used depends on whether server supports it. */
788                         LCONSOLE_ERROR_MSG(0x152, "Ignoring deprecated "
789                                                   "mount option 'acl'.\n");
790                         goto next;
791                 }
792                 tmp = ll_set_opt("noacl", s1, LL_SBI_ACL);
793                 if (tmp) {
794                         LCONSOLE_ERROR_MSG(0x152, "Ignoring deprecated "
795                                                   "mount option 'noacl'.\n");
796                         goto next;
797                 }
798 #else
799 #warning "{no}acl options have been deprecated since 1.8, please remove them"
800 #endif
801                 tmp = ll_set_opt("remote_client", s1, LL_SBI_RMT_CLIENT);
802                 if (tmp) {
803                         *flags |= tmp;
804                         goto next;
805                 }
806                 tmp = ll_set_opt("user_fid2path", s1, LL_SBI_USER_FID2PATH);
807                 if (tmp) {
808                         *flags |= tmp;
809                         goto next;
810                 }
811                 tmp = ll_set_opt("nouser_fid2path", s1, LL_SBI_USER_FID2PATH);
812                 if (tmp) {
813                         *flags &= ~tmp;
814                         goto next;
815                 }
816
817                 tmp = ll_set_opt("checksum", s1, LL_SBI_CHECKSUM);
818                 if (tmp) {
819                         *flags |= tmp;
820                         goto next;
821                 }
822                 tmp = ll_set_opt("nochecksum", s1, LL_SBI_CHECKSUM);
823                 if (tmp) {
824                         *flags &= ~tmp;
825                         goto next;
826                 }
827                 tmp = ll_set_opt("lruresize", s1, LL_SBI_LRU_RESIZE);
828                 if (tmp) {
829                         *flags |= tmp;
830                         goto next;
831                 }
832                 tmp = ll_set_opt("nolruresize", s1, LL_SBI_LRU_RESIZE);
833                 if (tmp) {
834                         *flags &= ~tmp;
835                         goto next;
836                 }
837                 tmp = ll_set_opt("lazystatfs", s1, LL_SBI_LAZYSTATFS);
838                 if (tmp) {
839                         *flags |= tmp;
840                         goto next;
841                 }
842                 tmp = ll_set_opt("nolazystatfs", s1, LL_SBI_LAZYSTATFS);
843                 if (tmp) {
844                         *flags &= ~tmp;
845                         goto next;
846                 }
847                 tmp = ll_set_opt("som_preview", s1, LL_SBI_SOM_PREVIEW);
848                 if (tmp) {
849                         *flags |= tmp;
850                         goto next;
851                 }
852                 tmp = ll_set_opt("32bitapi", s1, LL_SBI_32BIT_API);
853                 if (tmp) {
854                         *flags |= tmp;
855                         goto next;
856                 }
857                 tmp = ll_set_opt("verbose", s1, LL_SBI_VERBOSE);
858                 if (tmp) {
859                         *flags |= tmp;
860                         goto next;
861                 }
862                 tmp = ll_set_opt("noverbose", s1, LL_SBI_VERBOSE);
863                 if (tmp) {
864                         *flags &= ~tmp;
865                         goto next;
866                 }
867                 LCONSOLE_ERROR_MSG(0x152, "Unknown option '%s', won't mount.\n",
868                                    s1);
869                 RETURN(-EINVAL);
870
871 next:
872                 /* Find next opt */
873                 s2 = strchr(s1, ',');
874                 if (s2 == NULL)
875                         break;
876                 s1 = s2 + 1;
877         }
878         RETURN(0);
879 }
880
881 void ll_lli_init(struct ll_inode_info *lli)
882 {
883         lli->lli_inode_magic = LLI_INODE_MAGIC;
884         lli->lli_flags = 0;
885         lli->lli_ioepoch = 0;
886         lli->lli_maxbytes = MAX_LFS_FILESIZE;
887         cfs_spin_lock_init(&lli->lli_lock);
888         lli->lli_posix_acl = NULL;
889         lli->lli_remote_perms = NULL;
890         cfs_mutex_init(&lli->lli_rmtperm_mutex);
891         /* Do not set lli_fid, it has been initialized already. */
892         fid_zero(&lli->lli_pfid);
893         CFS_INIT_LIST_HEAD(&lli->lli_close_list);
894         CFS_INIT_LIST_HEAD(&lli->lli_oss_capas);
895         cfs_atomic_set(&lli->lli_open_count, 0);
896         lli->lli_mds_capa = NULL;
897         lli->lli_rmtperm_time = 0;
898         lli->lli_pending_och = NULL;
899         lli->lli_mds_read_och = NULL;
900         lli->lli_mds_write_och = NULL;
901         lli->lli_mds_exec_och = NULL;
902         lli->lli_open_fd_read_count = 0;
903         lli->lli_open_fd_write_count = 0;
904         lli->lli_open_fd_exec_count = 0;
905         cfs_mutex_init(&lli->lli_och_mutex);
906         cfs_spin_lock_init(&lli->lli_agl_lock);
907         lli->lli_has_smd = false;
908         lli->lli_clob = NULL;
909
910         LASSERT(lli->lli_vfs_inode.i_mode != 0);
911         if (S_ISDIR(lli->lli_vfs_inode.i_mode)) {
912                 cfs_mutex_init(&lli->lli_readdir_mutex);
913                 lli->lli_opendir_key = NULL;
914                 lli->lli_sai = NULL;
915                 lli->lli_sa_pos = 0;
916                 lli->lli_def_acl = NULL;
917                 cfs_spin_lock_init(&lli->lli_sa_lock);
918                 lli->lli_opendir_pid = 0;
919         } else {
920                 cfs_sema_init(&lli->lli_size_sem, 1);
921                 lli->lli_size_sem_owner = NULL;
922                 lli->lli_symlink_name = NULL;
923                 cfs_init_rwsem(&lli->lli_trunc_sem);
924                 cfs_mutex_init(&lli->lli_write_mutex);
925                 cfs_init_rwsem(&lli->lli_glimpse_sem);
926                 lli->lli_glimpse_time = 0;
927                 CFS_INIT_LIST_HEAD(&lli->lli_agl_list);
928                 lli->lli_agl_index = 0;
929                 lli->lli_async_rc = 0;
930         }
931         cfs_mutex_init(&lli->lli_layout_mutex);
932 }
933
934 static inline int ll_bdi_register(struct backing_dev_info *bdi)
935 {
936 #ifdef HAVE_BDI_REGISTER
937         static atomic_t ll_bdi_num = ATOMIC_INIT(0);
938
939 #ifdef HAVE_BDI_NAME
940         bdi->name = "lustre";
941 #endif
942         return bdi_register(bdi, NULL, "lustre-%d",
943                             atomic_inc_return(&ll_bdi_num));
944 #else
945         return 0;
946 #endif
947 }
948
949 int ll_fill_super(struct super_block *sb, struct vfsmount *mnt)
950 {
951         struct lustre_profile *lprof = NULL;
952         struct lustre_sb_info *lsi = s2lsi(sb);
953         struct ll_sb_info *sbi;
954         char  *dt = NULL, *md = NULL;
955         char  *profilenm = get_profile_name(sb);
956         struct config_llog_instance *cfg;
957         /* %p for void* in printf needs 16+2 characters: 0xffffffffffffffff */
958         const int instlen = sizeof(cfg->cfg_instance) * 2 + 2;
959         int    err;
960         ENTRY;
961
962         CDEBUG(D_VFSTRACE, "VFS Op: sb %p\n", sb);
963
964         OBD_ALLOC_PTR(cfg);
965         if (cfg == NULL)
966                 RETURN(-ENOMEM);
967
968         cfs_module_get();
969
970         /* client additional sb info */
971         lsi->lsi_llsbi = sbi = ll_init_sbi();
972         if (!sbi) {
973                 cfs_module_put(THIS_MODULE);
974                 OBD_FREE_PTR(cfg);
975                 RETURN(-ENOMEM);
976         }
977
978         err = ll_options(lsi->lsi_lmd->lmd_opts, &sbi->ll_flags);
979         if (err)
980                 GOTO(out_free, err);
981
982         err = ll_bdi_init(&lsi->lsi_bdi);
983         if (err)
984                 GOTO(out_free, err);
985         lsi->lsi_flags |= LSI_BDI_INITIALIZED;
986         lsi->lsi_bdi.capabilities = BDI_CAP_MAP_COPY;
987         err = ll_bdi_register(&lsi->lsi_bdi);
988         if (err)
989                 GOTO(out_free, err);
990
991 #ifdef HAVE_SB_BDI
992         sb->s_bdi = &lsi->lsi_bdi;
993 #endif
994
995         /* Generate a string unique to this super, in case some joker tries
996            to mount the same fs at two mount points.
997            Use the address of the super itself.*/
998         cfg->cfg_instance = sb;
999         cfg->cfg_uuid = lsi->lsi_llsbi->ll_sb_uuid;
1000         cfg->cfg_callback = class_config_llog_handler;
1001         /* set up client obds */
1002         err = lustre_process_log(sb, profilenm, cfg);
1003         if (err < 0) {
1004                 CERROR("Unable to process log: %d\n", err);
1005                 GOTO(out_free, err);
1006         }
1007
1008         /* Profile set with LCFG_MOUNTOPT so we can find our mdc and osc obds */
1009         lprof = class_get_profile(profilenm);
1010         if (lprof == NULL) {
1011                 LCONSOLE_ERROR_MSG(0x156, "The client profile '%s' could not be"
1012                                    " read from the MGS.  Does that filesystem "
1013                                    "exist?\n", profilenm);
1014                 GOTO(out_free, err = -EINVAL);
1015         }
1016         CDEBUG(D_CONFIG, "Found profile %s: mdc=%s osc=%s\n", profilenm,
1017                lprof->lp_md, lprof->lp_dt);
1018
1019         OBD_ALLOC(dt, strlen(lprof->lp_dt) + instlen + 2);
1020         if (!dt)
1021                 GOTO(out_free, err = -ENOMEM);
1022         sprintf(dt, "%s-%p", lprof->lp_dt, cfg->cfg_instance);
1023
1024         OBD_ALLOC(md, strlen(lprof->lp_md) + instlen + 2);
1025         if (!md)
1026                 GOTO(out_free, err = -ENOMEM);
1027         sprintf(md, "%s-%p", lprof->lp_md, cfg->cfg_instance);
1028
1029         /* connections, registrations, sb setup */
1030         err = client_common_fill_super(sb, md, dt, mnt);
1031
1032 out_free:
1033         if (md)
1034                 OBD_FREE(md, strlen(lprof->lp_md) + instlen + 2);
1035         if (dt)
1036                 OBD_FREE(dt, strlen(lprof->lp_dt) + instlen + 2);
1037         if (err)
1038                 ll_put_super(sb);
1039         else if (sbi->ll_flags & LL_SBI_VERBOSE)
1040                 LCONSOLE_WARN("Mounted %s\n", profilenm);
1041
1042         OBD_FREE_PTR(cfg);
1043         RETURN(err);
1044 } /* ll_fill_super */
1045
1046
1047 void lu_context_keys_dump(void);
1048
1049 void ll_put_super(struct super_block *sb)
1050 {
1051         struct config_llog_instance cfg;
1052         struct obd_device *obd;
1053         struct lustre_sb_info *lsi = s2lsi(sb);
1054         struct ll_sb_info *sbi = ll_s2sbi(sb);
1055         char *profilenm = get_profile_name(sb);
1056         int force = 1, next;
1057         ENTRY;
1058
1059         CDEBUG(D_VFSTRACE, "VFS Op: sb %p - %s\n", sb, profilenm);
1060
1061         ll_print_capa_stat(sbi);
1062
1063         cfg.cfg_instance = sb;
1064         lustre_end_log(sb, profilenm, &cfg);
1065
1066         if (sbi->ll_md_exp) {
1067                 obd = class_exp2obd(sbi->ll_md_exp);
1068                 if (obd)
1069                         force = obd->obd_force;
1070         }
1071
1072         /* We need to set force before the lov_disconnect in
1073            lustre_common_put_super, since l_d cleans up osc's as well. */
1074         if (force) {
1075                 next = 0;
1076                 while ((obd = class_devices_in_group(&sbi->ll_sb_uuid,
1077                                                      &next)) != NULL) {
1078                         obd->obd_force = force;
1079                 }
1080         }
1081
1082         if (sbi->ll_lcq) {
1083                 /* Only if client_common_fill_super succeeded */
1084                 client_common_put_super(sb);
1085         }
1086
1087         next = 0;
1088         while ((obd = class_devices_in_group(&sbi->ll_sb_uuid, &next)) !=NULL) {
1089                 class_manual_cleanup(obd);
1090         }
1091
1092         if (sbi->ll_flags & LL_SBI_VERBOSE)
1093                 LCONSOLE_WARN("Unmounted %s\n", profilenm ? profilenm : "");
1094
1095         if (profilenm)
1096                 class_del_profile(profilenm);
1097
1098         if (lsi->lsi_flags & LSI_BDI_INITIALIZED) {
1099                 ll_bdi_destroy(&lsi->lsi_bdi);
1100                 lsi->lsi_flags &= ~LSI_BDI_INITIALIZED;
1101         }
1102
1103         ll_free_sbi(sb);
1104         lsi->lsi_llsbi = NULL;
1105
1106         lustre_common_put_super(sb);
1107
1108         cl_env_cache_purge(~0);
1109
1110         cfs_module_put(THIS_MODULE);
1111
1112         EXIT;
1113 } /* client_put_super */
1114
1115 struct inode *ll_inode_from_lock(struct ldlm_lock *lock)
1116 {
1117         struct inode *inode = NULL;
1118         /* NOTE: we depend on atomic igrab() -bzzz */
1119         lock_res_and_lock(lock);
1120         if (lock->l_ast_data) {
1121                 struct ll_inode_info *lli = ll_i2info(lock->l_ast_data);
1122                 if (lli->lli_inode_magic == LLI_INODE_MAGIC) {
1123                         inode = igrab(lock->l_ast_data);
1124                 } else {
1125                         inode = lock->l_ast_data;
1126                         LDLM_DEBUG_LIMIT(inode->i_state & I_FREEING ?  D_INFO :
1127                                          D_WARNING, lock, "l_ast_data %p is "
1128                                          "bogus: magic %08x", lock->l_ast_data,
1129                                          lli->lli_inode_magic);
1130                         inode = NULL;
1131                 }
1132         }
1133         unlock_res_and_lock(lock);
1134         return inode;
1135 }
1136
1137 static int null_if_equal(struct ldlm_lock *lock, void *data)
1138 {
1139         if (data == lock->l_ast_data) {
1140                 lock->l_ast_data = NULL;
1141
1142                 if (lock->l_req_mode != lock->l_granted_mode)
1143                         LDLM_ERROR(lock,"clearing inode with ungranted lock");
1144         }
1145
1146         return LDLM_ITER_CONTINUE;
1147 }
1148
1149 void ll_clear_inode(struct inode *inode)
1150 {
1151         struct ll_inode_info *lli = ll_i2info(inode);
1152         struct ll_sb_info *sbi = ll_i2sbi(inode);
1153         ENTRY;
1154
1155         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
1156                inode->i_generation, inode);
1157
1158         if (S_ISDIR(inode->i_mode)) {
1159                 /* these should have been cleared in ll_file_release */
1160                 LASSERT(lli->lli_opendir_key == NULL);
1161                 LASSERT(lli->lli_sai == NULL);
1162                 LASSERT(lli->lli_opendir_pid == 0);
1163         }
1164
1165         ll_i2info(inode)->lli_flags &= ~LLIF_MDS_SIZE_LOCK;
1166         md_change_cbdata(sbi->ll_md_exp, ll_inode2fid(inode),
1167                          null_if_equal, inode);
1168
1169         LASSERT(!lli->lli_open_fd_write_count);
1170         LASSERT(!lli->lli_open_fd_read_count);
1171         LASSERT(!lli->lli_open_fd_exec_count);
1172
1173         if (lli->lli_mds_write_och)
1174                 ll_md_real_close(inode, FMODE_WRITE);
1175         if (lli->lli_mds_exec_och)
1176                 ll_md_real_close(inode, FMODE_EXEC);
1177         if (lli->lli_mds_read_och)
1178                 ll_md_real_close(inode, FMODE_READ);
1179
1180         if (S_ISLNK(inode->i_mode) && lli->lli_symlink_name) {
1181                 OBD_FREE(lli->lli_symlink_name,
1182                          strlen(lli->lli_symlink_name) + 1);
1183                 lli->lli_symlink_name = NULL;
1184         }
1185
1186         if (sbi->ll_flags & LL_SBI_RMT_CLIENT) {
1187                 LASSERT(lli->lli_posix_acl == NULL);
1188                 if (lli->lli_remote_perms) {
1189                         free_rmtperm_hash(lli->lli_remote_perms);
1190                         lli->lli_remote_perms = NULL;
1191                 }
1192         }
1193 #ifdef CONFIG_FS_POSIX_ACL
1194         else if (lli->lli_posix_acl) {
1195                 LASSERT(cfs_atomic_read(&lli->lli_posix_acl->a_refcount) == 1);
1196                 LASSERT(lli->lli_remote_perms == NULL);
1197                 posix_acl_release(lli->lli_posix_acl);
1198                 lli->lli_posix_acl = NULL;
1199         }
1200 #endif
1201         lli->lli_inode_magic = LLI_INODE_DEAD;
1202
1203         ll_clear_inode_capas(inode);
1204         if (!S_ISDIR(inode->i_mode))
1205                 LASSERT(cfs_list_empty(&lli->lli_agl_list));
1206
1207         /*
1208          * XXX This has to be done before lsm is freed below, because
1209          * cl_object still uses inode lsm.
1210          */
1211         cl_inode_fini(inode);
1212         lli->lli_has_smd = false;
1213
1214         EXIT;
1215 }
1216
1217 int ll_md_setattr(struct dentry *dentry, struct md_op_data *op_data,
1218                   struct md_open_data **mod)
1219 {
1220         struct lustre_md md;
1221         struct inode *inode = dentry->d_inode;
1222         struct ll_sb_info *sbi = ll_i2sbi(inode);
1223         struct ptlrpc_request *request = NULL;
1224         int rc, ia_valid;
1225         ENTRY;
1226
1227         op_data = ll_prep_md_op_data(op_data, inode, NULL, NULL, 0, 0,
1228                                      LUSTRE_OPC_ANY, NULL);
1229         if (IS_ERR(op_data))
1230                 RETURN(PTR_ERR(op_data));
1231
1232         rc = md_setattr(sbi->ll_md_exp, op_data, NULL, 0, NULL, 0,
1233                         &request, mod);
1234         if (rc) {
1235                 ptlrpc_req_finished(request);
1236                 if (rc == -ENOENT) {
1237                         inode->i_nlink = 0;
1238                         /* Unlinked special device node? Or just a race?
1239                          * Pretend we done everything. */
1240                         if (!S_ISREG(inode->i_mode) &&
1241                             !S_ISDIR(inode->i_mode)) {
1242                                 ia_valid = op_data->op_attr.ia_valid;
1243                                 op_data->op_attr.ia_valid &= ~TIMES_SET_FLAGS;
1244                                 rc = simple_setattr(dentry, &op_data->op_attr);
1245                                 op_data->op_attr.ia_valid = ia_valid;
1246                         }
1247                 } else if (rc != -EPERM && rc != -EACCES && rc != -ETXTBSY) {
1248                         CERROR("md_setattr fails: rc = %d\n", rc);
1249                 }
1250                 RETURN(rc);
1251         }
1252
1253         rc = md_get_lustre_md(sbi->ll_md_exp, request, sbi->ll_dt_exp,
1254                               sbi->ll_md_exp, &md);
1255         if (rc) {
1256                 ptlrpc_req_finished(request);
1257                 RETURN(rc);
1258         }
1259
1260         /* We want to adjust timestamps.
1261          * If there is at least some data in file, we cleared ATTR_SIZE
1262          * to avoid update size, otherwise it is important to do.(SOM case)
1263          * (bug 6196) */
1264         ia_valid = op_data->op_attr.ia_valid;
1265         /* Since we set ATTR_*_SET flags above, and already done permission
1266          * check, So don't let inode_change_ok() check it again.  */
1267         op_data->op_attr.ia_valid &= ~TIMES_SET_FLAGS;
1268         rc = simple_setattr(dentry, &op_data->op_attr);
1269         op_data->op_attr.ia_valid = ia_valid;
1270
1271         /* Extract epoch data if obtained. */
1272         op_data->op_handle = md.body->handle;
1273         op_data->op_ioepoch = md.body->ioepoch;
1274
1275         ll_update_inode(inode, &md);
1276         ptlrpc_req_finished(request);
1277
1278         RETURN(rc);
1279 }
1280
1281 /* Close IO epoch and send Size-on-MDS attribute update. */
1282 static int ll_setattr_done_writing(struct inode *inode,
1283                                    struct md_op_data *op_data,
1284                                    struct md_open_data *mod)
1285 {
1286         struct ll_inode_info *lli = ll_i2info(inode);
1287         int rc = 0;
1288         ENTRY;
1289
1290         LASSERT(op_data != NULL);
1291         if (!S_ISREG(inode->i_mode))
1292                 RETURN(0);
1293
1294         CDEBUG(D_INODE, "Epoch "LPU64" closed on "DFID" for truncate\n",
1295                op_data->op_ioepoch, PFID(&lli->lli_fid));
1296
1297         op_data->op_flags = MF_EPOCH_CLOSE;
1298         ll_done_writing_attr(inode, op_data);
1299         ll_pack_inode2opdata(inode, op_data, NULL);
1300
1301         rc = md_done_writing(ll_i2sbi(inode)->ll_md_exp, op_data, mod);
1302         if (rc == -EAGAIN) {
1303                 /* MDS has instructed us to obtain Size-on-MDS attribute
1304                  * from OSTs and send setattr to back to MDS. */
1305                 rc = ll_som_update(inode, op_data);
1306         } else if (rc) {
1307                 CERROR("inode %lu mdc truncate failed: rc = %d\n",
1308                        inode->i_ino, rc);
1309         }
1310         RETURN(rc);
1311 }
1312
1313 static int ll_setattr_ost(struct inode *inode, struct iattr *attr)
1314 {
1315         struct obd_capa *capa;
1316         int rc;
1317
1318         if (attr->ia_valid & ATTR_SIZE)
1319                 capa = ll_osscapa_get(inode, CAPA_OPC_OSS_TRUNC);
1320         else
1321                 capa = ll_mdscapa_get(inode);
1322
1323         rc = cl_setattr_ost(inode, attr, capa);
1324
1325         if (attr->ia_valid & ATTR_SIZE)
1326                 ll_truncate_free_capa(capa);
1327         else
1328                 capa_put(capa);
1329
1330         return rc;
1331 }
1332
1333 #ifndef HAVE_VFS_INODE_NEWSIZE_OK
1334 /**
1335  * inode_newsize_ok - may this inode be truncated to a given size
1336  * @inode:      the inode to be truncated
1337  * @offset:     the new size to assign to the inode
1338  * @Returns:    0 on success, -ve errno on failure
1339  *
1340  * inode_newsize_ok will check filesystem limits and ulimits to check that the
1341  * new inode size is within limits. inode_newsize_ok will also send SIGXFSZ
1342  * when necessary. Caller must not proceed with inode size change if failure is
1343  * returned. @inode must be a file (not directory), with appropriate
1344  * permissions to allow truncate (inode_newsize_ok does NOT check these
1345  * conditions).
1346  *
1347  * inode_newsize_ok must be called with i_mutex held.
1348  */
1349 int inode_newsize_ok(const struct inode *inode, loff_t offset)
1350 {
1351         if (inode->i_size < offset) {
1352                 unsigned long limit;
1353
1354                 limit = rlimit(RLIMIT_FSIZE);
1355                 if (limit != RLIM_INFINITY && offset > limit)
1356                         goto out_sig;
1357                 if (offset > inode->i_sb->s_maxbytes)
1358                         goto out_big;
1359         } else {
1360                 /*
1361                  * truncation of in-use swapfiles is disallowed - it would
1362                  * cause subsequent swapout to scribble on the now-freed
1363                  * blocks.
1364                  */
1365                 if (IS_SWAPFILE(inode))
1366                         return -ETXTBSY;
1367         }
1368
1369         return 0;
1370 out_sig:
1371         send_sig(SIGXFSZ, current, 0);
1372 out_big:
1373         return -EFBIG;
1374 }
1375 #endif
1376
1377 /* If this inode has objects allocated to it (lsm != NULL), then the OST
1378  * object(s) determine the file size and mtime.  Otherwise, the MDS will
1379  * keep these values until such a time that objects are allocated for it.
1380  * We do the MDS operations first, as it is checking permissions for us.
1381  * We don't to the MDS RPC if there is nothing that we want to store there,
1382  * otherwise there is no harm in updating mtime/atime on the MDS if we are
1383  * going to do an RPC anyways.
1384  *
1385  * If we are doing a truncate, we will send the mtime and ctime updates
1386  * to the OST with the punch RPC, otherwise we do an explicit setattr RPC.
1387  * I don't believe it is possible to get e.g. ATTR_MTIME_SET and ATTR_SIZE
1388  * at the same time.
1389  */
1390 int ll_setattr_raw(struct dentry *dentry, struct iattr *attr)
1391 {
1392         struct lov_stripe_md *lsm;
1393         struct inode *inode = dentry->d_inode;
1394         struct ll_inode_info *lli = ll_i2info(inode);
1395         struct md_op_data *op_data = NULL;
1396         struct md_open_data *mod = NULL;
1397         int ia_valid = attr->ia_valid;
1398         int rc = 0, rc1 = 0;
1399         ENTRY;
1400
1401         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu valid %x\n", inode->i_ino,
1402                attr->ia_valid);
1403
1404         if (ia_valid & ATTR_SIZE) {
1405                 /* Check new size against VFS/VM file size limit and rlimit */
1406                 rc = inode_newsize_ok(inode, attr->ia_size);
1407                 if (rc)
1408                         RETURN(rc);
1409
1410                 /* The maximum Lustre file size is variable, based on the
1411                  * OST maximum object size and number of stripes.  This
1412                  * needs another check in addition to the VFS check above. */
1413                 if (attr->ia_size > ll_file_maxbytes(inode)) {
1414                         CDEBUG(D_INODE,"file "DFID" too large %llu > "LPU64"\n",
1415                                PFID(&lli->lli_fid), attr->ia_size,
1416                                ll_file_maxbytes(inode));
1417                         RETURN(-EFBIG);
1418                 }
1419
1420                 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
1421         }
1422
1423         /* POSIX: check before ATTR_*TIME_SET set (from inode_change_ok) */
1424         if (ia_valid & TIMES_SET_FLAGS) {
1425                 if (cfs_curproc_fsuid() != inode->i_uid &&
1426                     !cfs_capable(CFS_CAP_FOWNER))
1427                         RETURN(-EPERM);
1428         }
1429
1430         /* We mark all of the fields "set" so MDS/OST does not re-set them */
1431         if (attr->ia_valid & ATTR_CTIME) {
1432                 attr->ia_ctime = CFS_CURRENT_TIME;
1433                 attr->ia_valid |= ATTR_CTIME_SET;
1434         }
1435         if (!(ia_valid & ATTR_ATIME_SET) && (attr->ia_valid & ATTR_ATIME)) {
1436                 attr->ia_atime = CFS_CURRENT_TIME;
1437                 attr->ia_valid |= ATTR_ATIME_SET;
1438         }
1439         if (!(ia_valid & ATTR_MTIME_SET) && (attr->ia_valid & ATTR_MTIME)) {
1440                 attr->ia_mtime = CFS_CURRENT_TIME;
1441                 attr->ia_valid |= ATTR_MTIME_SET;
1442         }
1443
1444         if (attr->ia_valid & (ATTR_MTIME | ATTR_CTIME))
1445                 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu, now = %lu\n",
1446                        LTIME_S(attr->ia_mtime), LTIME_S(attr->ia_ctime),
1447                        cfs_time_current_sec());
1448
1449         /* We always do an MDS RPC, even if we're only changing the size;
1450          * only the MDS knows whether truncate() should fail with -ETXTBUSY */
1451
1452         OBD_ALLOC_PTR(op_data);
1453         if (op_data == NULL)
1454                 RETURN(-ENOMEM);
1455
1456         if (!S_ISDIR(inode->i_mode)) {
1457                 if (ia_valid & ATTR_SIZE)
1458                         UP_WRITE_I_ALLOC_SEM(inode);
1459                 mutex_unlock(&inode->i_mutex);
1460                 cfs_down_write(&lli->lli_trunc_sem);
1461                 mutex_lock(&inode->i_mutex);
1462                 if (ia_valid & ATTR_SIZE)
1463                         DOWN_WRITE_I_ALLOC_SEM(inode);
1464         }
1465
1466         /* We need a steady stripe configuration for setattr to avoid
1467          * confusion. */
1468         lsm = ccc_inode_lsm_get(inode);
1469
1470         /* NB: ATTR_SIZE will only be set after this point if the size
1471          * resides on the MDS, ie, this file has no objects. */
1472         if (lsm != NULL)
1473                 attr->ia_valid &= ~ATTR_SIZE;
1474
1475         memcpy(&op_data->op_attr, attr, sizeof(*attr));
1476
1477         /* Open epoch for truncate. */
1478         if (exp_connect_som(ll_i2mdexp(inode)) &&
1479             (ia_valid & (ATTR_SIZE | ATTR_MTIME | ATTR_MTIME_SET)))
1480                 op_data->op_flags = MF_EPOCH_OPEN;
1481
1482         rc = ll_md_setattr(dentry, op_data, &mod);
1483         if (rc)
1484                 GOTO(out, rc);
1485
1486         ll_ioepoch_open(lli, op_data->op_ioepoch);
1487         if (lsm == NULL || !S_ISREG(inode->i_mode)) {
1488                 CDEBUG(D_INODE, "no lsm: not setting attrs on OST\n");
1489                 GOTO(out, rc = 0);
1490         }
1491
1492         if (ia_valid & ATTR_SIZE)
1493                 attr->ia_valid |= ATTR_SIZE;
1494         if (ia_valid & (ATTR_SIZE |
1495                         ATTR_ATIME | ATTR_ATIME_SET |
1496                         ATTR_MTIME | ATTR_MTIME_SET))
1497                 /* For truncate and utimes sending attributes to OSTs, setting
1498                  * mtime/atime to the past will be performed under PW [0:EOF]
1499                  * extent lock (new_size:EOF for truncate).  It may seem
1500                  * excessive to send mtime/atime updates to OSTs when not
1501                  * setting times to past, but it is necessary due to possible
1502                  * time de-synchronization between MDT inode and OST objects */
1503                 rc = ll_setattr_ost(inode, attr);
1504         EXIT;
1505 out:
1506         ccc_inode_lsm_put(inode, lsm);
1507         if (op_data) {
1508                 if (op_data->op_ioepoch) {
1509                         rc1 = ll_setattr_done_writing(inode, op_data, mod);
1510                         if (!rc)
1511                                 rc = rc1;
1512                 }
1513                 ll_finish_md_op_data(op_data);
1514         }
1515         if (!S_ISDIR(inode->i_mode))
1516                 cfs_up_write(&lli->lli_trunc_sem);
1517
1518         ll_stats_ops_tally(ll_i2sbi(inode), (ia_valid & ATTR_SIZE) ?
1519                            LPROC_LL_TRUNC : LPROC_LL_SETATTR, 1);
1520
1521         return rc;
1522 }
1523
1524 int ll_setattr(struct dentry *de, struct iattr *attr)
1525 {
1526         int mode = de->d_inode->i_mode;
1527
1528         if ((attr->ia_valid & (ATTR_CTIME|ATTR_SIZE|ATTR_MODE)) ==
1529                               (ATTR_CTIME|ATTR_SIZE|ATTR_MODE))
1530                 attr->ia_valid |= MDS_OPEN_OWNEROVERRIDE;
1531
1532         if (((attr->ia_valid & (ATTR_MODE|ATTR_FORCE|ATTR_SIZE)) ==
1533                                (ATTR_SIZE|ATTR_MODE)) &&
1534             (((mode & S_ISUID) && !(attr->ia_mode & S_ISUID)) ||
1535              (((mode & (S_ISGID|S_IXGRP)) == (S_ISGID|S_IXGRP)) &&
1536               !(attr->ia_mode & S_ISGID))))
1537                 attr->ia_valid |= ATTR_FORCE;
1538
1539         if ((mode & S_ISUID) &&
1540             !(attr->ia_mode & S_ISUID) &&
1541             !(attr->ia_valid & ATTR_KILL_SUID))
1542                 attr->ia_valid |= ATTR_KILL_SUID;
1543
1544         if (((mode & (S_ISGID|S_IXGRP)) == (S_ISGID|S_IXGRP)) &&
1545             !(attr->ia_mode & S_ISGID) &&
1546             !(attr->ia_valid & ATTR_KILL_SGID))
1547                 attr->ia_valid |= ATTR_KILL_SGID;
1548
1549         return ll_setattr_raw(de, attr);
1550 }
1551
1552 int ll_statfs_internal(struct super_block *sb, struct obd_statfs *osfs,
1553                        __u64 max_age, __u32 flags)
1554 {
1555         struct ll_sb_info *sbi = ll_s2sbi(sb);
1556         struct obd_statfs obd_osfs;
1557         int rc;
1558         ENTRY;
1559
1560         rc = obd_statfs(NULL, sbi->ll_md_exp, osfs, max_age, flags);
1561         if (rc) {
1562                 CERROR("md_statfs fails: rc = %d\n", rc);
1563                 RETURN(rc);
1564         }
1565
1566         osfs->os_type = sb->s_magic;
1567
1568         CDEBUG(D_SUPER, "MDC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1569                osfs->os_bavail, osfs->os_blocks, osfs->os_ffree,osfs->os_files);
1570
1571         if (sbi->ll_flags & LL_SBI_LAZYSTATFS)
1572                 flags |= OBD_STATFS_NODELAY;
1573
1574         rc = obd_statfs_rqset(sbi->ll_dt_exp, &obd_osfs, max_age, flags);
1575         if (rc) {
1576                 CERROR("obd_statfs fails: rc = %d\n", rc);
1577                 RETURN(rc);
1578         }
1579
1580         CDEBUG(D_SUPER, "OSC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1581                obd_osfs.os_bavail, obd_osfs.os_blocks, obd_osfs.os_ffree,
1582                obd_osfs.os_files);
1583
1584         osfs->os_bsize = obd_osfs.os_bsize;
1585         osfs->os_blocks = obd_osfs.os_blocks;
1586         osfs->os_bfree = obd_osfs.os_bfree;
1587         osfs->os_bavail = obd_osfs.os_bavail;
1588
1589         /* If we don't have as many objects free on the OST as inodes
1590          * on the MDS, we reduce the total number of inodes to
1591          * compensate, so that the "inodes in use" number is correct.
1592          */
1593         if (obd_osfs.os_ffree < osfs->os_ffree) {
1594                 osfs->os_files = (osfs->os_files - osfs->os_ffree) +
1595                         obd_osfs.os_ffree;
1596                 osfs->os_ffree = obd_osfs.os_ffree;
1597         }
1598
1599         RETURN(rc);
1600 }
1601 int ll_statfs(struct dentry *de, struct kstatfs *sfs)
1602 {
1603         struct super_block *sb = de->d_sb;
1604         struct obd_statfs osfs;
1605         int rc;
1606
1607         CDEBUG(D_VFSTRACE, "VFS Op: at "LPU64" jiffies\n", get_jiffies_64());
1608         ll_stats_ops_tally(ll_s2sbi(sb), LPROC_LL_STAFS, 1);
1609
1610         /* Some amount of caching on the client is allowed */
1611         rc = ll_statfs_internal(sb, &osfs,
1612                                 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
1613                                 0);
1614         if (rc)
1615                 return rc;
1616
1617         statfs_unpack(sfs, &osfs);
1618
1619         /* We need to downshift for all 32-bit kernels, because we can't
1620          * tell if the kernel is being called via sys_statfs64() or not.
1621          * Stop before overflowing f_bsize - in which case it is better
1622          * to just risk EOVERFLOW if caller is using old sys_statfs(). */
1623         if (sizeof(long) < 8) {
1624                 while (osfs.os_blocks > ~0UL && sfs->f_bsize < 0x40000000) {
1625                         sfs->f_bsize <<= 1;
1626
1627                         osfs.os_blocks >>= 1;
1628                         osfs.os_bfree >>= 1;
1629                         osfs.os_bavail >>= 1;
1630                 }
1631         }
1632
1633         sfs->f_blocks = osfs.os_blocks;
1634         sfs->f_bfree = osfs.os_bfree;
1635         sfs->f_bavail = osfs.os_bavail;
1636
1637         return 0;
1638 }
1639
1640 void ll_inode_size_lock(struct inode *inode)
1641 {
1642         struct ll_inode_info *lli;
1643
1644         LASSERT(!S_ISDIR(inode->i_mode));
1645
1646         lli = ll_i2info(inode);
1647         LASSERT(lli->lli_size_sem_owner != current);
1648         cfs_down(&lli->lli_size_sem);
1649         LASSERT(lli->lli_size_sem_owner == NULL);
1650         lli->lli_size_sem_owner = current;
1651 }
1652
1653 void ll_inode_size_unlock(struct inode *inode)
1654 {
1655         struct ll_inode_info *lli;
1656
1657         lli = ll_i2info(inode);
1658         LASSERT(lli->lli_size_sem_owner == current);
1659         lli->lli_size_sem_owner = NULL;
1660         cfs_up(&lli->lli_size_sem);
1661 }
1662
1663 void ll_update_inode(struct inode *inode, struct lustre_md *md)
1664 {
1665         struct ll_inode_info *lli = ll_i2info(inode);
1666         struct mdt_body *body = md->body;
1667         struct lov_stripe_md *lsm = md->lsm;
1668         struct ll_sb_info *sbi = ll_i2sbi(inode);
1669
1670         LASSERT ((lsm != NULL) == ((body->valid & OBD_MD_FLEASIZE) != 0));
1671         if (lsm != NULL) {
1672                 LASSERT(S_ISREG(inode->i_mode));
1673                 cfs_mutex_lock(&lli->lli_och_mutex);
1674                 CDEBUG(D_INODE, "adding lsm %p to inode %lu/%u(%p)\n",
1675                                 lsm, inode->i_ino, inode->i_generation, inode);
1676                 /* cl_file_inode_init must go before lli_has_smd or a race
1677                  * is possible where client thinks the file has stripes,
1678                  * but lov raid0 is not setup yet and parallel e.g.
1679                  * glimpse would try to use uninitialized lov */
1680                 if (cl_file_inode_init(inode, md) == 0)
1681                         lli->lli_has_smd = true;
1682                 cfs_mutex_unlock(&lli->lli_och_mutex);
1683
1684                 lli->lli_maxbytes = lsm->lsm_maxbytes;
1685                 if (lli->lli_maxbytes > MAX_LFS_FILESIZE)
1686                         lli->lli_maxbytes = MAX_LFS_FILESIZE;
1687                 if (md->lsm != NULL)
1688                         obd_free_memmd(ll_i2dtexp(inode), &md->lsm);
1689         }
1690
1691         if (sbi->ll_flags & LL_SBI_RMT_CLIENT) {
1692                 if (body->valid & OBD_MD_FLRMTPERM)
1693                         ll_update_remote_perm(inode, md->remote_perm);
1694         }
1695 #ifdef CONFIG_FS_POSIX_ACL
1696         else if (body->valid & OBD_MD_FLACL) {
1697                 cfs_spin_lock(&lli->lli_lock);
1698                 if (lli->lli_posix_acl)
1699                         posix_acl_release(lli->lli_posix_acl);
1700                 lli->lli_posix_acl = md->posix_acl;
1701                 cfs_spin_unlock(&lli->lli_lock);
1702         }
1703 #endif
1704         inode->i_ino = cl_fid_build_ino(&body->fid1, 0);
1705         inode->i_generation = cl_fid_build_gen(&body->fid1);
1706
1707         if (body->valid & OBD_MD_FLATIME) {
1708                 if (body->atime > LTIME_S(inode->i_atime))
1709                         LTIME_S(inode->i_atime) = body->atime;
1710                 lli->lli_lvb.lvb_atime = body->atime;
1711         }
1712         if (body->valid & OBD_MD_FLMTIME) {
1713                 if (body->mtime > LTIME_S(inode->i_mtime)) {
1714                         CDEBUG(D_INODE, "setting ino %lu mtime from %lu "
1715                                "to "LPU64"\n", inode->i_ino,
1716                                LTIME_S(inode->i_mtime), body->mtime);
1717                         LTIME_S(inode->i_mtime) = body->mtime;
1718                 }
1719                 lli->lli_lvb.lvb_mtime = body->mtime;
1720         }
1721         if (body->valid & OBD_MD_FLCTIME) {
1722                 if (body->ctime > LTIME_S(inode->i_ctime))
1723                         LTIME_S(inode->i_ctime) = body->ctime;
1724                 lli->lli_lvb.lvb_ctime = body->ctime;
1725         }
1726         if (body->valid & OBD_MD_FLMODE)
1727                 inode->i_mode = (inode->i_mode & S_IFMT)|(body->mode & ~S_IFMT);
1728         if (body->valid & OBD_MD_FLTYPE)
1729                 inode->i_mode = (inode->i_mode & ~S_IFMT)|(body->mode & S_IFMT);
1730         LASSERT(inode->i_mode != 0);
1731         if (S_ISREG(inode->i_mode)) {
1732                 inode->i_blkbits = min(PTLRPC_MAX_BRW_BITS + 1, LL_MAX_BLKSIZE_BITS);
1733         } else {
1734                 inode->i_blkbits = inode->i_sb->s_blocksize_bits;
1735         }
1736         if (body->valid & OBD_MD_FLUID)
1737                 inode->i_uid = body->uid;
1738         if (body->valid & OBD_MD_FLGID)
1739                 inode->i_gid = body->gid;
1740         if (body->valid & OBD_MD_FLFLAGS)
1741                 inode->i_flags = ll_ext_to_inode_flags(body->flags);
1742         if (body->valid & OBD_MD_FLNLINK)
1743                 inode->i_nlink = body->nlink;
1744         if (body->valid & OBD_MD_FLRDEV)
1745                 inode->i_rdev = old_decode_dev(body->rdev);
1746
1747         if (body->valid & OBD_MD_FLID) {
1748                 /* FID shouldn't be changed! */
1749                 if (fid_is_sane(&lli->lli_fid)) {
1750                         LASSERTF(lu_fid_eq(&lli->lli_fid, &body->fid1),
1751                                  "Trying to change FID "DFID
1752                                  " to the "DFID", inode %lu/%u(%p)\n",
1753                                  PFID(&lli->lli_fid), PFID(&body->fid1),
1754                                  inode->i_ino, inode->i_generation, inode);
1755                 } else
1756                         lli->lli_fid = body->fid1;
1757         }
1758
1759         LASSERT(fid_seq(&lli->lli_fid) != 0);
1760
1761         if (body->valid & OBD_MD_FLSIZE) {
1762                 if (exp_connect_som(ll_i2mdexp(inode)) &&
1763                     S_ISREG(inode->i_mode) && lli->lli_has_smd) {
1764                         struct lustre_handle lockh;
1765                         ldlm_mode_t mode;
1766
1767                         /* As it is possible a blocking ast has been processed
1768                          * by this time, we need to check there is an UPDATE
1769                          * lock on the client and set LLIF_MDS_SIZE_LOCK holding
1770                          * it. */
1771                         mode = ll_take_md_lock(inode, MDS_INODELOCK_UPDATE,
1772                                                &lockh);
1773                         if (mode) {
1774                                 if (lli->lli_flags & (LLIF_DONE_WRITING |
1775                                                       LLIF_EPOCH_PENDING |
1776                                                       LLIF_SOM_DIRTY)) {
1777                                         CERROR("ino %lu flags %u still has "
1778                                                "size authority! do not trust "
1779                                                "the size got from MDS\n",
1780                                                inode->i_ino, lli->lli_flags);
1781                                 } else {
1782                                         /* Use old size assignment to avoid
1783                                          * deadlock bz14138 & bz14326 */
1784                                         i_size_write(inode, body->size);
1785                                         lli->lli_flags |= LLIF_MDS_SIZE_LOCK;
1786                                 }
1787                                 ldlm_lock_decref(&lockh, mode);
1788                         }
1789                 } else {
1790                         /* Use old size assignment to avoid
1791                          * deadlock bz14138 & bz14326 */
1792                         i_size_write(inode, body->size);
1793
1794                         CDEBUG(D_VFSTRACE, "inode=%lu, updating i_size %llu\n",
1795                                inode->i_ino, (unsigned long long)body->size);
1796                 }
1797
1798                 if (body->valid & OBD_MD_FLBLOCKS)
1799                         inode->i_blocks = body->blocks;
1800         }
1801
1802         if (body->valid & OBD_MD_FLMDSCAPA) {
1803                 LASSERT(md->mds_capa);
1804                 ll_add_capa(inode, md->mds_capa);
1805         }
1806         if (body->valid & OBD_MD_FLOSSCAPA) {
1807                 LASSERT(md->oss_capa);
1808                 ll_add_capa(inode, md->oss_capa);
1809         }
1810 }
1811
1812 void ll_read_inode2(struct inode *inode, void *opaque)
1813 {
1814         struct lustre_md *md = opaque;
1815         struct ll_inode_info *lli = ll_i2info(inode);
1816         ENTRY;
1817
1818         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
1819                PFID(&lli->lli_fid), inode);
1820
1821         LASSERT(!lli->lli_has_smd);
1822
1823         /* Core attributes from the MDS first.  This is a new inode, and
1824          * the VFS doesn't zero times in the core inode so we have to do
1825          * it ourselves.  They will be overwritten by either MDS or OST
1826          * attributes - we just need to make sure they aren't newer. */
1827         LTIME_S(inode->i_mtime) = 0;
1828         LTIME_S(inode->i_atime) = 0;
1829         LTIME_S(inode->i_ctime) = 0;
1830         inode->i_rdev = 0;
1831         ll_update_inode(inode, md);
1832
1833         /* OIDEBUG(inode); */
1834
1835         /* initializing backing dev info. */
1836         inode->i_mapping->backing_dev_info = &s2lsi(inode->i_sb)->lsi_bdi;
1837
1838
1839         if (S_ISREG(inode->i_mode)) {
1840                 struct ll_sb_info *sbi = ll_i2sbi(inode);
1841                 inode->i_op = &ll_file_inode_operations;
1842                 inode->i_fop = sbi->ll_fop;
1843                 inode->i_mapping->a_ops = (struct address_space_operations *)&ll_aops;
1844                 EXIT;
1845         } else if (S_ISDIR(inode->i_mode)) {
1846                 inode->i_op = &ll_dir_inode_operations;
1847                 inode->i_fop = &ll_dir_operations;
1848                 inode->i_mapping->a_ops = (struct address_space_operations *)&ll_dir_aops;
1849                 EXIT;
1850         } else if (S_ISLNK(inode->i_mode)) {
1851                 inode->i_op = &ll_fast_symlink_inode_operations;
1852                 EXIT;
1853         } else {
1854                 inode->i_op = &ll_special_inode_operations;
1855
1856                 init_special_inode(inode, inode->i_mode,
1857                                    kdev_t_to_nr(inode->i_rdev));
1858
1859                 EXIT;
1860         }
1861 }
1862
1863 void ll_delete_inode(struct inode *inode)
1864 {
1865         struct cl_inode_info *lli = cl_i2info(inode);
1866         struct ll_sb_info    *sbi = ll_i2sbi(inode);
1867         int rc;
1868         ENTRY;
1869
1870         rc = obd_fid_delete(sbi->ll_md_exp, ll_inode2fid(inode));
1871         if (rc)
1872                 CERROR("fid_delete() failed, rc %d\n", rc);
1873
1874         if (S_ISREG(inode->i_mode) && lli->lli_clob != NULL)
1875                 /* discard all dirty pages before truncating them, required by
1876                  * osc_extent implementation at LU-1030. */
1877                 cl_sync_file_range(inode, 0, OBD_OBJECT_EOF, CL_FSYNC_DISCARD);
1878
1879         truncate_inode_pages(&inode->i_data, 0);
1880
1881         /* Workaround for LU-118 */
1882         if (inode->i_data.nrpages) {
1883                 TREE_READ_LOCK_IRQ(&inode->i_data);
1884                 TREE_READ_UNLOCK_IRQ(&inode->i_data);
1885                 LASSERTF(inode->i_data.nrpages == 0,
1886                          "inode=%lu/%u(%p) nrpages=%lu, see "
1887                          "http://jira.whamcloud.com/browse/LU-118\n",
1888                          inode->i_ino, inode->i_generation, inode,
1889                          inode->i_data.nrpages);
1890         }
1891         /* Workaround end */
1892
1893 #ifdef HAVE_SBOPS_EVICT_INODE
1894         ll_clear_inode(inode);
1895         end_writeback(inode);
1896 #else
1897         clear_inode(inode);
1898 #endif
1899
1900         EXIT;
1901 }
1902
1903 int ll_iocontrol(struct inode *inode, struct file *file,
1904                  unsigned int cmd, unsigned long arg)
1905 {
1906         struct ll_sb_info *sbi = ll_i2sbi(inode);
1907         struct ptlrpc_request *req = NULL;
1908         int rc, flags = 0;
1909         ENTRY;
1910
1911         switch(cmd) {
1912         case FSFILT_IOC_GETFLAGS: {
1913                 struct mdt_body *body;
1914                 struct md_op_data *op_data;
1915
1916                 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL,
1917                                              0, 0, LUSTRE_OPC_ANY,
1918                                              NULL);
1919                 if (IS_ERR(op_data))
1920                         RETURN(PTR_ERR(op_data));
1921
1922                 op_data->op_valid = OBD_MD_FLFLAGS;
1923                 rc = md_getattr(sbi->ll_md_exp, op_data, &req);
1924                 ll_finish_md_op_data(op_data);
1925                 if (rc) {
1926                         CERROR("failure %d inode %lu\n", rc, inode->i_ino);
1927                         RETURN(-abs(rc));
1928                 }
1929
1930                 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
1931
1932                 flags = body->flags;
1933
1934                 ptlrpc_req_finished(req);
1935
1936                 RETURN(put_user(flags, (int *)arg));
1937         }
1938         case FSFILT_IOC_SETFLAGS: {
1939                 struct lov_stripe_md *lsm;
1940                 struct obd_info oinfo = { { { 0 } } };
1941                 struct md_op_data *op_data;
1942
1943                 if (get_user(flags, (int *)arg))
1944                         RETURN(-EFAULT);
1945
1946                 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
1947                                              LUSTRE_OPC_ANY, NULL);
1948                 if (IS_ERR(op_data))
1949                         RETURN(PTR_ERR(op_data));
1950
1951                 ((struct ll_iattr *)&op_data->op_attr)->ia_attr_flags = flags;
1952                 op_data->op_attr.ia_valid |= ATTR_ATTR_FLAG;
1953                 rc = md_setattr(sbi->ll_md_exp, op_data,
1954                                 NULL, 0, NULL, 0, &req, NULL);
1955                 ll_finish_md_op_data(op_data);
1956                 ptlrpc_req_finished(req);
1957                 if (rc)
1958                         RETURN(rc);
1959
1960                 OBDO_ALLOC(oinfo.oi_oa);
1961                 if (!oinfo.oi_oa)
1962                         RETURN(-ENOMEM);
1963
1964                 lsm = ccc_inode_lsm_get(inode);
1965                 if (lsm == NULL) {
1966                         inode->i_flags = ll_ext_to_inode_flags(flags);
1967                         OBDO_FREE(oinfo.oi_oa);
1968                         RETURN(0);
1969                 }
1970
1971                 oinfo.oi_md = lsm;
1972                 oinfo.oi_oa->o_id = lsm->lsm_object_id;
1973                 oinfo.oi_oa->o_seq = lsm->lsm_object_seq;
1974                 oinfo.oi_oa->o_flags = flags;
1975                 oinfo.oi_oa->o_valid = OBD_MD_FLID | OBD_MD_FLFLAGS |
1976                                        OBD_MD_FLGROUP;
1977                 oinfo.oi_capa = ll_mdscapa_get(inode);
1978                 obdo_set_parent_fid(oinfo.oi_oa, &ll_i2info(inode)->lli_fid);
1979                 rc = obd_setattr_rqset(sbi->ll_dt_exp, &oinfo, NULL);
1980                 capa_put(oinfo.oi_capa);
1981                 OBDO_FREE(oinfo.oi_oa);
1982                 ccc_inode_lsm_put(inode, lsm);
1983
1984                 if (rc && rc != -EPERM && rc != -EACCES)
1985                         CERROR("osc_setattr_async fails: rc = %d\n", rc);
1986
1987                 RETURN(rc);
1988         }
1989         default:
1990                 RETURN(-ENOSYS);
1991         }
1992
1993         RETURN(0);
1994 }
1995
1996 int ll_flush_ctx(struct inode *inode)
1997 {
1998         struct ll_sb_info  *sbi = ll_i2sbi(inode);
1999
2000         CDEBUG(D_SEC, "flush context for user %d\n", cfs_curproc_uid());
2001
2002         obd_set_info_async(NULL, sbi->ll_md_exp,
2003                            sizeof(KEY_FLUSH_CTX), KEY_FLUSH_CTX,
2004                            0, NULL, NULL);
2005         obd_set_info_async(NULL, sbi->ll_dt_exp,
2006                            sizeof(KEY_FLUSH_CTX), KEY_FLUSH_CTX,
2007                            0, NULL, NULL);
2008         return 0;
2009 }
2010
2011 /* umount -f client means force down, don't save state */
2012 #ifdef HAVE_UMOUNTBEGIN_VFSMOUNT
2013 void ll_umount_begin(struct vfsmount *vfsmnt, int flags)
2014 {
2015         struct super_block *sb = vfsmnt->mnt_sb;
2016 #else
2017 void ll_umount_begin(struct super_block *sb)
2018 {
2019 #endif
2020         struct ll_sb_info *sbi = ll_s2sbi(sb);
2021         struct obd_device *obd;
2022         struct obd_ioctl_data *ioc_data;
2023         ENTRY;
2024
2025 #ifdef HAVE_UMOUNTBEGIN_VFSMOUNT
2026         if (!(flags & MNT_FORCE)) {
2027                 EXIT;
2028                 return;
2029         }
2030 #endif
2031
2032         CDEBUG(D_VFSTRACE, "VFS Op: superblock %p count %d active %d\n", sb,
2033                sb->s_count, atomic_read(&sb->s_active));
2034
2035         obd = class_exp2obd(sbi->ll_md_exp);
2036         if (obd == NULL) {
2037                 CERROR("Invalid MDC connection handle "LPX64"\n",
2038                        sbi->ll_md_exp->exp_handle.h_cookie);
2039                 EXIT;
2040                 return;
2041         }
2042         obd->obd_force = 1;
2043
2044         obd = class_exp2obd(sbi->ll_dt_exp);
2045         if (obd == NULL) {
2046                 CERROR("Invalid LOV connection handle "LPX64"\n",
2047                        sbi->ll_dt_exp->exp_handle.h_cookie);
2048                 EXIT;
2049                 return;
2050         }
2051         obd->obd_force = 1;
2052
2053         OBD_ALLOC_PTR(ioc_data);
2054         if (ioc_data) {
2055                 obd_iocontrol(IOC_OSC_SET_ACTIVE, sbi->ll_md_exp,
2056                               sizeof ioc_data, ioc_data, NULL);
2057
2058                 obd_iocontrol(IOC_OSC_SET_ACTIVE, sbi->ll_dt_exp,
2059                               sizeof ioc_data, ioc_data, NULL);
2060
2061                 OBD_FREE_PTR(ioc_data);
2062         }
2063
2064
2065         /* Really, we'd like to wait until there are no requests outstanding,
2066          * and then continue.  For now, we just invalidate the requests,
2067          * schedule() and sleep one second if needed, and hope.
2068          */
2069         cfs_schedule();
2070 #ifdef HAVE_UMOUNTBEGIN_VFSMOUNT
2071         if (atomic_read(&vfsmnt->mnt_count) > 2) {
2072                 cfs_schedule_timeout_and_set_state(CFS_TASK_INTERRUPTIBLE,
2073                                                    cfs_time_seconds(1));
2074                 if (atomic_read(&vfsmnt->mnt_count) > 2)
2075                         LCONSOLE_WARN("Mount still busy with %d refs! You "
2076                                       "may try to umount it a bit later\n",
2077                                       atomic_read(&vfsmnt->mnt_count));
2078         }
2079 #endif
2080
2081         EXIT;
2082 }
2083
2084 int ll_remount_fs(struct super_block *sb, int *flags, char *data)
2085 {
2086         struct ll_sb_info *sbi = ll_s2sbi(sb);
2087         char *profilenm = get_profile_name(sb);
2088         int err;
2089         __u32 read_only;
2090
2091         if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY)) {
2092                 read_only = *flags & MS_RDONLY;
2093                 err = obd_set_info_async(NULL, sbi->ll_md_exp,
2094                                          sizeof(KEY_READ_ONLY),
2095                                          KEY_READ_ONLY, sizeof(read_only),
2096                                          &read_only, NULL);
2097                 if (err) {
2098                         LCONSOLE_WARN("Failed to remount %s %s (%d)\n",
2099                                       profilenm, read_only ?
2100                                       "read-only" : "read-write", err);
2101                         return err;
2102                 }
2103
2104                 if (read_only)
2105                         sb->s_flags |= MS_RDONLY;
2106                 else
2107                         sb->s_flags &= ~MS_RDONLY;
2108
2109                 if (sbi->ll_flags & LL_SBI_VERBOSE)
2110                         LCONSOLE_WARN("Remounted %s %s\n", profilenm,
2111                                       read_only ?  "read-only" : "read-write");
2112         }
2113         return 0;
2114 }
2115
2116 int ll_prep_inode(struct inode **inode,
2117                   struct ptlrpc_request *req,
2118                   struct super_block *sb)
2119 {
2120         struct ll_sb_info *sbi = NULL;
2121         struct lustre_md md;
2122         __u64 ibits;
2123         int rc;
2124         ENTRY;
2125
2126         LASSERT(*inode || sb);
2127         sbi = sb ? ll_s2sbi(sb) : ll_i2sbi(*inode);
2128         rc = md_get_lustre_md(sbi->ll_md_exp, req, sbi->ll_dt_exp,
2129                               sbi->ll_md_exp, &md);
2130         if (rc)
2131                 RETURN(rc);
2132
2133         if (*inode) {
2134                 ll_update_inode(*inode, &md);
2135         } else {
2136                 LASSERT(sb != NULL);
2137
2138                 /*
2139                  * At this point server returns to client's same fid as client
2140                  * generated for creating. So using ->fid1 is okay here.
2141                  */
2142                 LASSERT(fid_is_sane(&md.body->fid1));
2143
2144                 *inode = ll_iget(sb, cl_fid_build_ino(&md.body->fid1, 0), &md);
2145                 if (*inode == NULL || IS_ERR(*inode)) {
2146                         if (md.lsm)
2147                                 obd_free_memmd(sbi->ll_dt_exp, &md.lsm);
2148 #ifdef CONFIG_FS_POSIX_ACL
2149                         if (md.posix_acl) {
2150                                 posix_acl_release(md.posix_acl);
2151                                 md.posix_acl = NULL;
2152                         }
2153 #endif
2154                         rc = IS_ERR(*inode) ? PTR_ERR(*inode) : -ENOMEM;
2155                         *inode = NULL;
2156                         CERROR("new_inode -fatal: rc %d\n", rc);
2157                         GOTO(out, rc);
2158                 }
2159         }
2160
2161         /* sanity check for LAYOUT lock. */
2162         ibits = MDS_INODELOCK_LAYOUT;
2163         if (S_ISREG(md.body->mode) && sbi->ll_flags & LL_SBI_LAYOUT_LOCK &&
2164             md.lsm != NULL && !ll_have_md_lock(*inode, &ibits, LCK_MINMODE)) {
2165                 char *fsname = ll_get_fsname(*inode);
2166                 CERROR("%s: inode "DFID" (%p) layout lock not granted.\n",
2167                         fsname, PFID(ll_inode2fid(*inode)),
2168                         *inode);
2169                 if (fsname)
2170                         OBD_FREE(fsname, MGS_PARAM_MAXLEN);
2171         }
2172
2173 out:
2174         md_free_lustre_md(sbi->ll_md_exp, &md);
2175         RETURN(rc);
2176 }
2177
2178 int ll_obd_statfs(struct inode *inode, void *arg)
2179 {
2180         struct ll_sb_info *sbi = NULL;
2181         struct obd_export *exp;
2182         char *buf = NULL;
2183         struct obd_ioctl_data *data = NULL;
2184         __u32 type;
2185         __u32 flags;
2186         int len = 0, rc;
2187
2188         if (!inode || !(sbi = ll_i2sbi(inode)))
2189                 GOTO(out_statfs, rc = -EINVAL);
2190
2191         rc = obd_ioctl_getdata(&buf, &len, arg);
2192         if (rc)
2193                 GOTO(out_statfs, rc);
2194
2195         data = (void*)buf;
2196         if (!data->ioc_inlbuf1 || !data->ioc_inlbuf2 ||
2197             !data->ioc_pbuf1 || !data->ioc_pbuf2)
2198                 GOTO(out_statfs, rc = -EINVAL);
2199
2200         if (data->ioc_inllen1 != sizeof(__u32) ||
2201             data->ioc_inllen2 != sizeof(__u32) ||
2202             data->ioc_plen1 != sizeof(struct obd_statfs) ||
2203             data->ioc_plen2 != sizeof(struct obd_uuid))
2204                 GOTO(out_statfs, rc = -EINVAL);
2205
2206         memcpy(&type, data->ioc_inlbuf1, sizeof(__u32));
2207         if (type & LL_STATFS_LMV)
2208                 exp = sbi->ll_md_exp;
2209         else if (type & LL_STATFS_LOV)
2210                 exp = sbi->ll_dt_exp;
2211         else
2212                 GOTO(out_statfs, rc = -ENODEV);
2213
2214         flags = (type & LL_STATFS_NODELAY) ? OBD_STATFS_NODELAY : 0;
2215         rc = obd_iocontrol(IOC_OBD_STATFS, exp, len, buf, &flags);
2216         if (rc)
2217                 GOTO(out_statfs, rc);
2218 out_statfs:
2219         if (buf)
2220                 obd_ioctl_freedata(buf, len);
2221         return rc;
2222 }
2223
2224 int ll_process_config(struct lustre_cfg *lcfg)
2225 {
2226         char *ptr;
2227         void *sb;
2228         struct lprocfs_static_vars lvars;
2229         unsigned long x;
2230         int rc = 0;
2231
2232         lprocfs_llite_init_vars(&lvars);
2233
2234         /* The instance name contains the sb: lustre-client-aacfe000 */
2235         ptr = strrchr(lustre_cfg_string(lcfg, 0), '-');
2236         if (!ptr || !*(++ptr))
2237                 return -EINVAL;
2238         if (sscanf(ptr, "%lx", &x) != 1)
2239                 return -EINVAL;
2240         sb = (void *)x;
2241         /* This better be a real Lustre superblock! */
2242         LASSERT(s2lsi((struct super_block *)sb)->lsi_lmd->lmd_magic == LMD_MAGIC);
2243
2244         /* Note we have not called client_common_fill_super yet, so
2245            proc fns must be able to handle that! */
2246         rc = class_process_proc_param(PARAM_LLITE, lvars.obd_vars,
2247                                       lcfg, sb);
2248         if (rc > 0)
2249                 rc = 0;
2250         return(rc);
2251 }
2252
2253 /* this function prepares md_op_data hint for passing ot down to MD stack. */
2254 struct md_op_data * ll_prep_md_op_data(struct md_op_data *op_data,
2255                                        struct inode *i1, struct inode *i2,
2256                                        const char *name, int namelen,
2257                                        int mode, __u32 opc, void *data)
2258 {
2259         LASSERT(i1 != NULL);
2260
2261         if (namelen > ll_i2sbi(i1)->ll_namelen)
2262                 return ERR_PTR(-ENAMETOOLONG);
2263
2264         if (op_data == NULL)
2265                 OBD_ALLOC_PTR(op_data);
2266
2267         if (op_data == NULL)
2268                 return ERR_PTR(-ENOMEM);
2269
2270         ll_i2gids(op_data->op_suppgids, i1, i2);
2271         op_data->op_fid1 = *ll_inode2fid(i1);
2272         op_data->op_capa1 = ll_mdscapa_get(i1);
2273
2274         if (i2) {
2275                 op_data->op_fid2 = *ll_inode2fid(i2);
2276                 op_data->op_capa2 = ll_mdscapa_get(i2);
2277         } else {
2278                 fid_zero(&op_data->op_fid2);
2279                 op_data->op_capa2 = NULL;
2280         }
2281
2282         op_data->op_name = name;
2283         op_data->op_namelen = namelen;
2284         op_data->op_mode = mode;
2285         op_data->op_mod_time = cfs_time_current_sec();
2286         op_data->op_fsuid = cfs_curproc_fsuid();
2287         op_data->op_fsgid = cfs_curproc_fsgid();
2288         op_data->op_cap = cfs_curproc_cap_pack();
2289         op_data->op_bias = MDS_CHECK_SPLIT;
2290         op_data->op_opc = opc;
2291         op_data->op_mds = 0;
2292         op_data->op_data = data;
2293
2294         /* If the file is being opened after mknod() (normally due to NFS)
2295          * try to use the default stripe data from parent directory for
2296          * allocating OST objects.  Try to pass the parent FID to MDS. */
2297         if (opc == LUSTRE_OPC_CREATE && i1 == i2 && S_ISREG(i2->i_mode) &&
2298             !ll_i2info(i2)->lli_has_smd) {
2299                 struct ll_inode_info *lli = ll_i2info(i2);
2300
2301                 cfs_spin_lock(&lli->lli_lock);
2302                 if (likely(!lli->lli_has_smd && !fid_is_zero(&lli->lli_pfid)))
2303                         op_data->op_fid1 = lli->lli_pfid;
2304                 cfs_spin_unlock(&lli->lli_lock);
2305                 /** We ignore parent's capability temporary. */
2306         }
2307
2308         return op_data;
2309 }
2310
2311 void ll_finish_md_op_data(struct md_op_data *op_data)
2312 {
2313         capa_put(op_data->op_capa1);
2314         capa_put(op_data->op_capa2);
2315         OBD_FREE_PTR(op_data);
2316 }
2317
2318 int ll_show_options(struct seq_file *seq, struct vfsmount *vfs)
2319 {
2320         struct ll_sb_info *sbi;
2321
2322         LASSERT((seq != NULL) && (vfs != NULL));
2323         sbi = ll_s2sbi(vfs->mnt_sb);
2324
2325         if (sbi->ll_flags & LL_SBI_NOLCK)
2326                 seq_puts(seq, ",nolock");
2327
2328         if (sbi->ll_flags & LL_SBI_FLOCK)
2329                 seq_puts(seq, ",flock");
2330
2331         if (sbi->ll_flags & LL_SBI_LOCALFLOCK)
2332                 seq_puts(seq, ",localflock");
2333
2334         if (sbi->ll_flags & LL_SBI_USER_XATTR)
2335                 seq_puts(seq, ",user_xattr");
2336
2337         if (sbi->ll_flags & LL_SBI_LAZYSTATFS)
2338                 seq_puts(seq, ",lazystatfs");
2339
2340         if (sbi->ll_flags & LL_SBI_USER_FID2PATH)
2341                 seq_puts(seq, ",user_fid2path");
2342
2343         RETURN(0);
2344 }
2345
2346 /**
2347  * Get obd name by cmd, and copy out to user space
2348  */
2349 int ll_get_obd_name(struct inode *inode, unsigned int cmd, unsigned long arg)
2350 {
2351         struct ll_sb_info *sbi = ll_i2sbi(inode);
2352         struct obd_device *obd;
2353         ENTRY;
2354
2355         if (cmd == OBD_IOC_GETDTNAME)
2356                 obd = class_exp2obd(sbi->ll_dt_exp);
2357         else if (cmd == OBD_IOC_GETMDNAME)
2358                 obd = class_exp2obd(sbi->ll_md_exp);
2359         else
2360                 RETURN(-EINVAL);
2361
2362         if (!obd)
2363                 RETURN(-ENOENT);
2364
2365         if (cfs_copy_to_user((void *)arg, obd->obd_name,
2366                              strlen(obd->obd_name) + 1))
2367                 RETURN(-EFAULT);
2368
2369         RETURN(0);
2370 }