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