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