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