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