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