Whamcloud - gitweb
Move random uuid functions to prng.c
[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         ll_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         ll_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                                 cfg.cfg_flags |= CFG_F_SERVER146;
925                         }
926                         OBD_FREE(oldname, oldnamelen);
927                 }
928         }
929         /* end COMPAT_146 */
930         if (err < 0) {
931                 CERROR("Unable to process log: %d\n", err);
932                 GOTO(out_free, err);
933         }
934
935         lprof = class_get_profile(profilenm);
936         if (lprof == NULL) {
937                 LCONSOLE_ERROR("The client profile '%s' could not be read "
938                                "from the MGS.  Does that filesystem exist?\n",
939                                profilenm);
940                 GOTO(out_free, err = -EINVAL);
941         }
942         CDEBUG(D_CONFIG, "Found profile %s: mdc=%s osc=%s\n", profilenm, 
943                lprof->lp_mdc, lprof->lp_osc);
944
945         OBD_ALLOC(osc, strlen(lprof->lp_osc) +
946                   strlen(ll_instance) + 2);
947         if (!osc) 
948                 GOTO(out_free, err = -ENOMEM);
949         sprintf(osc, "%s-%s", lprof->lp_osc, ll_instance);
950
951         OBD_ALLOC(mdc, strlen(lprof->lp_mdc) +
952                   strlen(ll_instance) + 2);
953         if (!mdc) 
954                 GOTO(out_free, err = -ENOMEM);
955         sprintf(mdc, "%s-%s", lprof->lp_mdc, ll_instance);
956   
957         /* connections, registrations, sb setup */
958         err = client_common_fill_super(sb, mdc, osc);
959   
960 out_free:
961         if (mdc)
962                 OBD_FREE(mdc, strlen(mdc) + 1);
963         if (osc)
964                 OBD_FREE(osc, strlen(osc) + 1);
965         if (err) 
966                 ll_put_super(sb);
967         else
968                 LCONSOLE_WARN("Client %s has started\n", profilenm);        
969
970         RETURN(err);
971 } /* ll_fill_super */
972
973
974 void ll_put_super(struct super_block *sb)
975 {
976         struct config_llog_instance cfg;
977         char   ll_instance[sizeof(sb) * 2 + 1];
978         struct obd_device *obd;
979         struct lustre_sb_info *lsi = s2lsi(sb);
980         struct ll_sb_info *sbi = ll_s2sbi(sb);
981         char *profilenm = get_profile_name(sb);
982         int force = 1, next;
983         ENTRY;
984
985         CDEBUG(D_VFSTRACE, "VFS Op: sb %p - %s\n", sb, profilenm);
986         
987         sprintf(ll_instance, "%p", sb);
988         cfg.cfg_instance = ll_instance;
989         lustre_end_log(sb, NULL, &cfg);
990         
991         if (sbi->ll_mdc_exp) {
992                 obd = class_exp2obd(sbi->ll_mdc_exp);
993                 if (obd) 
994                         force = obd->obd_no_recov;
995         }
996         
997         /* We need to set force before the lov_disconnect in 
998            lustre_common_put_super, since l_d cleans up osc's as well. */
999         if (force) {
1000                 next = 0;
1001                 while ((obd = class_devices_in_group(&sbi->ll_sb_uuid,
1002                                                      &next)) != NULL) {
1003                         obd->obd_force = force;
1004                 }
1005         }                       
1006
1007         if (sbi->ll_lcq) {
1008                 /* Only if client_common_fill_super succeeded */
1009                 client_common_put_super(sb);
1010         }
1011                 
1012         next = 0;
1013         while ((obd = class_devices_in_group(&sbi->ll_sb_uuid, &next)) !=NULL) {
1014                 class_manual_cleanup(obd);
1015         }                       
1016         
1017         if (profilenm) 
1018                 class_del_profile(profilenm);
1019
1020         ll_free_sbi(sb);
1021         lsi->lsi_llsbi = NULL;
1022
1023         lustre_common_put_super(sb);
1024
1025         LCONSOLE_WARN("client %s umount complete\n", ll_instance);
1026         
1027         cfs_module_put();
1028
1029         EXIT;
1030 } /* client_put_super */
1031
1032 #ifdef HAVE_REGISTER_CACHE
1033 #include <linux/cache_def.h>
1034 #ifdef HAVE_CACHE_RETURN_INT
1035 static int
1036 #else
1037 static void
1038 #endif
1039 ll_shrink_cache(int priority, unsigned int gfp_mask)
1040 {
1041         struct ll_sb_info *sbi;
1042         int count = 0;
1043
1044         list_for_each_entry(sbi, &ll_super_blocks, ll_list)
1045                 count += llap_shrink_cache(sbi, priority);
1046
1047 #ifdef HAVE_CACHE_RETURN_INT
1048         return count;
1049 #endif
1050 }
1051
1052 struct cache_definition ll_cache_definition = {
1053         .name = "llap_cache",
1054         .shrink = ll_shrink_cache
1055 };
1056 #endif /* HAVE_REGISTER_CACHE */
1057
1058 struct inode *ll_inode_from_lock(struct ldlm_lock *lock)
1059 {
1060         struct inode *inode = NULL;
1061         /* NOTE: we depend on atomic igrab() -bzzz */
1062         lock_res_and_lock(lock);
1063         if (lock->l_ast_data) {
1064                 struct ll_inode_info *lli = ll_i2info(lock->l_ast_data);
1065                 if (lli->lli_inode_magic == LLI_INODE_MAGIC) {
1066                         inode = igrab(lock->l_ast_data);
1067                 } else {
1068                         inode = lock->l_ast_data;
1069                         ldlm_lock_debug(NULL, inode->i_state & I_FREEING ?
1070                                                 D_INFO : D_WARNING,
1071                                         lock, __FILE__, __func__, __LINE__,
1072                                         "l_ast_data %p is bogus: magic %08x",
1073                                         lock->l_ast_data, lli->lli_inode_magic);
1074                         inode = NULL;
1075                 }
1076         }
1077         unlock_res_and_lock(lock);
1078         return inode;
1079 }
1080
1081 static int null_if_equal(struct ldlm_lock *lock, void *data)
1082 {
1083         if (data == lock->l_ast_data) {
1084                 lock->l_ast_data = NULL;
1085
1086                 if (lock->l_req_mode != lock->l_granted_mode)
1087                         LDLM_ERROR(lock,"clearing inode with ungranted lock");
1088         }
1089
1090         return LDLM_ITER_CONTINUE;
1091 }
1092
1093 void ll_clear_inode(struct inode *inode)
1094 {
1095         struct ll_fid fid;
1096         struct ll_inode_info *lli = ll_i2info(inode);
1097         struct ll_sb_info *sbi = ll_i2sbi(inode);
1098         ENTRY;
1099
1100         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
1101                inode->i_generation, inode);
1102
1103         ll_inode2fid(&fid, inode);
1104         clear_bit(LLI_F_HAVE_MDS_SIZE_LOCK, &lli->lli_flags);
1105         mdc_change_cbdata(sbi->ll_mdc_exp, &fid, null_if_equal, inode);
1106
1107         LASSERT(!lli->lli_open_fd_write_count);
1108         LASSERT(!lli->lli_open_fd_read_count);
1109         LASSERT(!lli->lli_open_fd_exec_count);
1110
1111         if (lli->lli_mds_write_och)
1112                 ll_mdc_real_close(inode, FMODE_WRITE);
1113         if (lli->lli_mds_exec_och) {
1114                 if (!FMODE_EXEC)
1115                         CERROR("No FMODE exec, bug exec och is present for "
1116                                "inode %ld\n", inode->i_ino);
1117                 ll_mdc_real_close(inode, FMODE_EXEC);
1118         }
1119         if (lli->lli_mds_read_och)
1120                 ll_mdc_real_close(inode, FMODE_READ);
1121
1122
1123         if (lli->lli_smd) {
1124                 obd_change_cbdata(sbi->ll_osc_exp, lli->lli_smd,
1125                                   null_if_equal, inode);
1126
1127                 obd_free_memmd(sbi->ll_osc_exp, &lli->lli_smd);
1128                 lli->lli_smd = NULL;
1129         }
1130
1131         if (lli->lli_symlink_name) {
1132                 OBD_FREE(lli->lli_symlink_name,
1133                          strlen(lli->lli_symlink_name) + 1);
1134                 lli->lli_symlink_name = NULL;
1135         }
1136
1137 #ifdef CONFIG_FS_POSIX_ACL
1138         if (lli->lli_posix_acl) {
1139                 LASSERT(atomic_read(&lli->lli_posix_acl->a_refcount) == 1);
1140                 posix_acl_release(lli->lli_posix_acl);
1141                 lli->lli_posix_acl = NULL;
1142         }
1143 #endif
1144
1145         lli->lli_inode_magic = LLI_INODE_DEAD;
1146
1147         spin_lock(&sbi->ll_deathrow_lock);
1148         list_del_init(&lli->lli_dead_list);
1149         spin_unlock(&sbi->ll_deathrow_lock);
1150
1151         EXIT;
1152 }
1153
1154 /* If this inode has objects allocated to it (lsm != NULL), then the OST
1155  * object(s) determine the file size and mtime.  Otherwise, the MDS will
1156  * keep these values until such a time that objects are allocated for it.
1157  * We do the MDS operations first, as it is checking permissions for us.
1158  * We don't to the MDS RPC if there is nothing that we want to store there,
1159  * otherwise there is no harm in updating mtime/atime on the MDS if we are
1160  * going to do an RPC anyways.
1161  *
1162  * If we are doing a truncate, we will send the mtime and ctime updates
1163  * to the OST with the punch RPC, otherwise we do an explicit setattr RPC.
1164  * I don't believe it is possible to get e.g. ATTR_MTIME_SET and ATTR_SIZE
1165  * at the same time.
1166  */
1167 int ll_setattr_raw(struct inode *inode, struct iattr *attr)
1168 {
1169         struct ll_inode_info *lli = ll_i2info(inode);
1170         struct lov_stripe_md *lsm = lli->lli_smd;
1171         struct ll_sb_info *sbi = ll_i2sbi(inode);
1172         struct ptlrpc_request *request = NULL;
1173         struct mdc_op_data op_data;
1174         struct lustre_md md;
1175         int ia_valid = attr->ia_valid;
1176         int rc = 0;
1177         ENTRY;
1178
1179         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu valid %x\n", inode->i_ino,
1180                attr->ia_valid);
1181         lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_SETATTR);
1182
1183         if (ia_valid & ATTR_SIZE) {
1184                 if (attr->ia_size > ll_file_maxbytes(inode)) {
1185                         CDEBUG(D_INODE, "file too large %llu > "LPU64"\n",
1186                                attr->ia_size, ll_file_maxbytes(inode));
1187                         RETURN(-EFBIG);
1188                 }
1189
1190                 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
1191         }
1192
1193         /* POSIX: check before ATTR_*TIME_SET set (from inode_change_ok) */
1194         if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET)) {
1195                 if (current->fsuid != inode->i_uid && !capable(CAP_FOWNER))
1196                         RETURN(-EPERM);
1197         }
1198
1199         /* We mark all of the fields "set" so MDS/OST does not re-set them */
1200         if (attr->ia_valid & ATTR_CTIME) {
1201                 attr->ia_ctime = CURRENT_TIME;
1202                 attr->ia_valid |= ATTR_CTIME_SET;
1203         }
1204         if (!(ia_valid & ATTR_ATIME_SET) && (attr->ia_valid & ATTR_ATIME)) {
1205                 attr->ia_atime = CURRENT_TIME;
1206                 attr->ia_valid |= ATTR_ATIME_SET;
1207         }
1208         if (!(ia_valid & ATTR_MTIME_SET) && (attr->ia_valid & ATTR_MTIME)) {
1209                 attr->ia_mtime = CURRENT_TIME;
1210                 attr->ia_valid |= ATTR_MTIME_SET;
1211         }
1212         if ((attr->ia_valid & ATTR_CTIME) && !(attr->ia_valid & ATTR_MTIME)) {
1213                 /* To avoid stale mtime on mds, obtain it from ost and send 
1214                    to mds. */
1215                 rc = ll_glimpse_size(inode, 0);
1216                 if (rc)
1217                         RETURN(rc);
1218
1219                 attr->ia_valid |= ATTR_MTIME_SET | ATTR_MTIME;
1220                 attr->ia_mtime = inode->i_mtime;
1221         }
1222
1223         if (attr->ia_valid & (ATTR_MTIME | ATTR_CTIME))
1224                 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu, now = %lu\n",
1225                        LTIME_S(attr->ia_mtime), LTIME_S(attr->ia_ctime),
1226                        CURRENT_SECONDS);
1227
1228         /* NB: ATTR_SIZE will only be set after this point if the size
1229          * resides on the MDS, ie, this file has no objects. */
1230         if (lsm)
1231                 attr->ia_valid &= ~ATTR_SIZE;
1232
1233         /* We always do an MDS RPC, even if we're only changing the size;
1234          * only the MDS knows whether truncate() should fail with -ETXTBUSY */
1235         ll_prepare_mdc_op_data(&op_data, inode, NULL, NULL, 0, 0);
1236
1237         rc = mdc_setattr(sbi->ll_mdc_exp, &op_data,
1238                          attr, NULL, 0, NULL, 0, &request);
1239
1240         if (rc) {
1241                 ptlrpc_req_finished(request);
1242                 if (rc == -ENOENT) {
1243                         inode->i_nlink = 0;
1244                         /* Unlinked special device node?  Or just a race?
1245                          * Pretend we done everything. */
1246                         if (!S_ISREG(inode->i_mode) &&
1247                             !S_ISDIR(inode->i_mode))
1248                                 rc = inode_setattr(inode, attr);
1249                 } else if (rc != -EPERM && rc != -EACCES && rc != -ETXTBSY)
1250                         CERROR("mdc_setattr fails: rc = %d\n", rc);
1251                 RETURN(rc);
1252         }
1253
1254         rc = mdc_req2lustre_md(request, REPLY_REC_OFF, sbi->ll_osc_exp, &md);
1255         if (rc) {
1256                 ptlrpc_req_finished(request);
1257                 RETURN(rc);
1258         }
1259
1260         /* We call inode_setattr to adjust timestamps.
1261          * If there is at least some data in file, we cleared ATTR_SIZE above to
1262          * avoid invoking vmtruncate, otherwise it is important to call
1263          * vmtruncate in inode_setattr to update inode->i_size (bug 6196) */
1264         rc = inode_setattr(inode, attr);
1265
1266         ll_update_inode(inode, &md);
1267         ptlrpc_req_finished(request);
1268
1269         if (!lsm || !S_ISREG(inode->i_mode)) {
1270                 CDEBUG(D_INODE, "no lsm: not setting attrs on OST\n");
1271                 RETURN(rc);
1272         }
1273
1274         /* We really need to get our PW lock before we change inode->i_size.
1275          * If we don't we can race with other i_size updaters on our node, like
1276          * ll_file_read.  We can also race with i_size propogation to other
1277          * nodes through dirtying and writeback of final cached pages.  This
1278          * last one is especially bad for racing o_append users on other
1279          * nodes. */
1280         if (ia_valid & ATTR_SIZE) {
1281                 ldlm_policy_data_t policy = { .l_extent = {attr->ia_size,
1282                                                            OBD_OBJECT_EOF } };
1283                 struct lustre_handle lockh = { 0 };
1284                 int err, ast_flags = 0;
1285                 /* XXX when we fix the AST intents to pass the discard-range
1286                  * XXX extent, make ast_flags always LDLM_AST_DISCARD_DATA
1287                  * XXX here. */
1288                 if (attr->ia_size == 0)
1289                         ast_flags = LDLM_AST_DISCARD_DATA;
1290
1291                 UNLOCK_INODE_MUTEX(inode);
1292                 UP_WRITE_I_ALLOC_SEM(inode);
1293                 rc = ll_extent_lock(NULL, inode, lsm, LCK_PW, &policy, &lockh,
1294                                     ast_flags);
1295 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1296                 DOWN_WRITE_I_ALLOC_SEM(inode);
1297                 LOCK_INODE_MUTEX(inode);
1298 #else
1299                 LOCK_INODE_MUTEX(inode);
1300                 DOWN_WRITE_I_ALLOC_SEM(inode);
1301 #endif
1302                 if (rc != 0)
1303                         RETURN(rc);
1304
1305                 /* Only ll_inode_size_lock is taken at this level.
1306                  * lov_stripe_lock() is grabbed by ll_truncate() only over
1307                  * call to obd_adjust_kms().  If vmtruncate returns 0, then
1308                  * ll_truncate dropped ll_inode_size_lock() */
1309                 ll_inode_size_lock(inode, 0);
1310                 rc = vmtruncate(inode, attr->ia_size);
1311                 if (rc != 0) {
1312                         LASSERT(atomic_read(&lli->lli_size_sem.count) <= 0);
1313                         ll_inode_size_unlock(inode, 0);
1314                 }
1315
1316                 err = ll_extent_unlock(NULL, inode, lsm, LCK_PW, &lockh);
1317                 if (err) {
1318                         CERROR("ll_extent_unlock failed: %d\n", err);
1319                         if (!rc)
1320                                 rc = err;
1321                 }
1322         } else if (ia_valid & (ATTR_MTIME | ATTR_MTIME_SET)) {
1323                 obd_flag flags;
1324                 struct obd_info oinfo = { { { 0 } } };
1325                 struct obdo *oa = obdo_alloc();
1326
1327                 CDEBUG(D_INODE, "set mtime on OST inode %lu to %lu\n",
1328                        inode->i_ino, LTIME_S(attr->ia_mtime));
1329
1330                 if (oa) {
1331                         oa->o_id = lsm->lsm_object_id;
1332                         oa->o_valid = OBD_MD_FLID;
1333
1334                         flags = OBD_MD_FLTYPE | OBD_MD_FLATIME |
1335                                 OBD_MD_FLMTIME | OBD_MD_FLCTIME |
1336                                 OBD_MD_FLFID | OBD_MD_FLGENER;
1337
1338                         obdo_from_inode(oa, inode, flags);
1339
1340                         oinfo.oi_oa = oa;
1341                         oinfo.oi_md = lsm;
1342
1343                         rc = obd_setattr_rqset(sbi->ll_osc_exp, &oinfo, NULL);
1344                         if (rc)
1345                                 CERROR("obd_setattr_async fails: rc=%d\n", rc);
1346                         obdo_free(oa);
1347                 } else {
1348                         rc = -ENOMEM;
1349                 }
1350         }
1351         RETURN(rc);
1352 }
1353
1354 int ll_setattr(struct dentry *de, struct iattr *attr)
1355 {
1356         ll_vfs_ops_tally(ll_i2sbi(de->d_inode), VFS_OPS_SETATTR);
1357
1358         if ((attr->ia_valid & (ATTR_CTIME|ATTR_SIZE|ATTR_MODE)) ==
1359             (ATTR_CTIME|ATTR_SIZE|ATTR_MODE))
1360                 attr->ia_valid |= MDS_OPEN_OWNEROVERRIDE;
1361
1362         return ll_setattr_raw(de->d_inode, attr);
1363 }
1364
1365 int ll_statfs_internal(struct super_block *sb, struct obd_statfs *osfs,
1366                        __u64 max_age)
1367 {
1368         struct ll_sb_info *sbi = ll_s2sbi(sb);
1369         struct obd_statfs obd_osfs;
1370         int rc;
1371         ENTRY;
1372
1373         rc = obd_statfs(class_exp2obd(sbi->ll_mdc_exp), osfs, max_age);
1374         if (rc) {
1375                 CERROR("mdc_statfs fails: rc = %d\n", rc);
1376                 RETURN(rc);
1377         }
1378
1379         osfs->os_type = sb->s_magic;
1380
1381         CDEBUG(D_SUPER, "MDC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1382                osfs->os_bavail, osfs->os_blocks, osfs->os_ffree,osfs->os_files);
1383
1384         rc = obd_statfs_rqset(class_exp2obd(sbi->ll_osc_exp),
1385                               &obd_osfs, max_age);
1386         if (rc) {
1387                 CERROR("obd_statfs fails: rc = %d\n", rc);
1388                 RETURN(rc);
1389         }
1390
1391         CDEBUG(D_SUPER, "OSC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1392                obd_osfs.os_bavail, obd_osfs.os_blocks, obd_osfs.os_ffree,
1393                obd_osfs.os_files);
1394
1395         osfs->os_blocks = obd_osfs.os_blocks;
1396         osfs->os_bfree = obd_osfs.os_bfree;
1397         osfs->os_bavail = obd_osfs.os_bavail;
1398
1399         /* If we don't have as many objects free on the OST as inodes
1400          * on the MDS, we reduce the total number of inodes to
1401          * compensate, so that the "inodes in use" number is correct.
1402          */
1403         if (obd_osfs.os_ffree < osfs->os_ffree) {
1404                 osfs->os_files = (osfs->os_files - osfs->os_ffree) +
1405                         obd_osfs.os_ffree;
1406                 osfs->os_ffree = obd_osfs.os_ffree;
1407         }
1408
1409         RETURN(rc);
1410 }
1411 #ifndef HAVE_STATFS_DENTRY_PARAM
1412 int ll_statfs(struct super_block *sb, struct kstatfs *sfs)
1413 {
1414 #else
1415 int ll_statfs(struct dentry *de, struct kstatfs *sfs)
1416 {
1417         struct super_block *sb = de->d_sb;
1418 #endif
1419         struct obd_statfs osfs;
1420         int rc;
1421
1422         CDEBUG(D_VFSTRACE, "VFS Op: at "LPU64" jiffies\n", get_jiffies_64());
1423         lprocfs_counter_incr(ll_s2sbi(sb)->ll_stats, LPROC_LL_STAFS);
1424
1425         /* For now we will always get up-to-date statfs values, but in the
1426          * future we may allow some amount of caching on the client (e.g.
1427          * from QOS or lprocfs updates). */
1428         rc = ll_statfs_internal(sb, &osfs, cfs_time_current_64() - 1);
1429         if (rc)
1430                 return rc;
1431
1432         statfs_unpack(sfs, &osfs);
1433
1434         /* We need to downshift for all 32-bit kernels, because we can't
1435          * tell if the kernel is being called via sys_statfs64() or not.
1436          * Stop before overflowing f_bsize - in which case it is better
1437          * to just risk EOVERFLOW if caller is using old sys_statfs(). */
1438         if (sizeof(long) < 8) {
1439                 while (osfs.os_blocks > ~0UL && sfs->f_bsize < 0x40000000) {
1440                         sfs->f_bsize <<= 1;
1441
1442                         osfs.os_blocks >>= 1;
1443                         osfs.os_bfree >>= 1;
1444                         osfs.os_bavail >>= 1;
1445                 }
1446         }
1447
1448         sfs->f_blocks = osfs.os_blocks;
1449         sfs->f_bfree = osfs.os_bfree;
1450         sfs->f_bavail = osfs.os_bavail;
1451
1452         return 0;
1453 }
1454
1455 void ll_inode_size_lock(struct inode *inode, int lock_lsm)
1456 {
1457         struct ll_inode_info *lli;
1458         struct lov_stripe_md *lsm;
1459
1460         lli = ll_i2info(inode);
1461         LASSERT(lli->lli_size_sem_owner != current);
1462         down(&lli->lli_size_sem);
1463         LASSERT(lli->lli_size_sem_owner == NULL);
1464         lli->lli_size_sem_owner = current;
1465         lsm = lli->lli_smd;
1466         LASSERTF(lsm != NULL || lock_lsm == 0, "lsm %p, lock_lsm %d\n",
1467                  lsm, lock_lsm);
1468         if (lock_lsm)
1469                 lov_stripe_lock(lsm);
1470 }
1471
1472 void ll_inode_size_unlock(struct inode *inode, int unlock_lsm)
1473 {
1474         struct ll_inode_info *lli;
1475         struct lov_stripe_md *lsm;
1476
1477         lli = ll_i2info(inode);
1478         lsm = lli->lli_smd;
1479         LASSERTF(lsm != NULL || unlock_lsm == 0, "lsm %p, lock_lsm %d\n",
1480                  lsm, unlock_lsm);
1481         if (unlock_lsm)
1482                 lov_stripe_unlock(lsm);
1483         LASSERT(lli->lli_size_sem_owner == current);
1484         lli->lli_size_sem_owner = NULL;
1485         up(&lli->lli_size_sem);
1486 }
1487
1488 static void ll_replace_lsm(struct inode *inode, struct lov_stripe_md *lsm)
1489 {
1490         struct ll_inode_info *lli = ll_i2info(inode);
1491
1492         dump_lsm(D_INODE, lsm);
1493         dump_lsm(D_INODE, lli->lli_smd);
1494         LASSERTF(lsm->lsm_magic == LOV_MAGIC_JOIN,
1495                  "lsm must be joined lsm %p\n", lsm);
1496         obd_free_memmd(ll_i2obdexp(inode), &lli->lli_smd);
1497         CDEBUG(D_INODE, "replace lsm %p to lli_smd %p for inode %lu%u(%p)\n",
1498                lsm, lli->lli_smd, inode->i_ino, inode->i_generation, inode);
1499         lli->lli_smd = lsm;
1500         lli->lli_maxbytes = lsm->lsm_maxbytes;
1501         if (lli->lli_maxbytes > PAGE_CACHE_MAXBYTES)
1502                 lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
1503 }
1504
1505 void ll_update_inode(struct inode *inode, struct lustre_md *md)
1506 {
1507         struct ll_inode_info *lli = ll_i2info(inode);
1508         struct mds_body *body = md->body;
1509         struct lov_stripe_md *lsm = md->lsm;
1510
1511         LASSERT ((lsm != NULL) == ((body->valid & OBD_MD_FLEASIZE) != 0));
1512         if (lsm != NULL) {
1513                 if (lli->lli_smd == NULL) {
1514                         if (lsm->lsm_magic != LOV_MAGIC &&
1515                             lsm->lsm_magic != LOV_MAGIC_JOIN) {
1516                                 dump_lsm(D_ERROR, lsm);
1517                                 LBUG();
1518                         }
1519                         CDEBUG(D_INODE, "adding lsm %p to inode %lu/%u(%p)\n",
1520                                lsm, inode->i_ino, inode->i_generation, inode);
1521                         /* ll_inode_size_lock() requires it is only called
1522                          * with lli_smd != NULL or lock_lsm == 0 or we can
1523                          * race between lock/unlock.  bug 9547 */
1524                         lli->lli_smd = lsm;
1525                         lli->lli_maxbytes = lsm->lsm_maxbytes;
1526                         if (lli->lli_maxbytes > PAGE_CACHE_MAXBYTES)
1527                                 lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
1528                 } else {
1529                         if (lli->lli_smd->lsm_magic == lsm->lsm_magic &&
1530                              lli->lli_smd->lsm_stripe_count ==
1531                                         lsm->lsm_stripe_count) {
1532                                 if (lov_stripe_md_cmp(lli->lli_smd, lsm)) {
1533                                         CERROR("lsm mismatch for inode %ld\n",
1534                                                 inode->i_ino);
1535                                         CERROR("lli_smd:\n");
1536                                         dump_lsm(D_ERROR, lli->lli_smd);
1537                                         CERROR("lsm:\n");
1538                                         dump_lsm(D_ERROR, lsm);
1539                                         LBUG();
1540                                 }
1541                         } else
1542                                 ll_replace_lsm(inode, lsm);
1543                 }
1544                 if (lli->lli_smd != lsm)
1545                         obd_free_memmd(ll_i2obdexp(inode), &lsm);
1546         }
1547
1548 #ifdef CONFIG_FS_POSIX_ACL
1549         LASSERT(!md->posix_acl || (body->valid & OBD_MD_FLACL));
1550         if (body->valid & OBD_MD_FLACL) {
1551                 spin_lock(&lli->lli_lock);
1552                 if (lli->lli_posix_acl)
1553                         posix_acl_release(lli->lli_posix_acl);
1554                 lli->lli_posix_acl = md->posix_acl;
1555                 spin_unlock(&lli->lli_lock);
1556         }
1557 #endif
1558
1559         if (body->valid & OBD_MD_FLID)
1560                 inode->i_ino = body->ino;
1561         if (body->valid & OBD_MD_FLATIME &&
1562             body->atime > LTIME_S(inode->i_atime))
1563                 LTIME_S(inode->i_atime) = body->atime;
1564         
1565         /* mtime is always updated with ctime, but can be set in past.
1566            As write and utime(2) may happen within 1 second, and utime's
1567            mtime has a priority over write's one, so take mtime from mds 
1568            for the same ctimes. */
1569         if (body->valid & OBD_MD_FLCTIME &&
1570             body->ctime >= LTIME_S(inode->i_ctime)) {
1571                 LTIME_S(inode->i_ctime) = body->ctime;
1572                 if (body->valid & OBD_MD_FLMTIME) {
1573                         CDEBUG(D_INODE, "setting ino %lu mtime "
1574                                "from %lu to "LPU64"\n", inode->i_ino, 
1575                                LTIME_S(inode->i_mtime), body->mtime);
1576                         LTIME_S(inode->i_mtime) = body->mtime;
1577                 }
1578         }
1579         if (body->valid & OBD_MD_FLMODE)
1580                 inode->i_mode = (inode->i_mode & S_IFMT)|(body->mode & ~S_IFMT);
1581         if (body->valid & OBD_MD_FLTYPE)
1582                 inode->i_mode = (inode->i_mode & ~S_IFMT)|(body->mode & S_IFMT);
1583         if (S_ISREG(inode->i_mode)) {
1584                 inode->i_blkbits = min(PTLRPC_MAX_BRW_BITS+1, LL_MAX_BLKSIZE_BITS);
1585         } else {
1586                 inode->i_blkbits = inode->i_sb->s_blocksize_bits;
1587         }
1588 #ifdef HAVE_INODE_BLKSIZE
1589         inode->i_blksize = 1<<inode->i_blkbits;
1590 #endif
1591         if (body->valid & OBD_MD_FLUID)
1592                 inode->i_uid = body->uid;
1593         if (body->valid & OBD_MD_FLGID)
1594                 inode->i_gid = body->gid;
1595         if (body->valid & OBD_MD_FLFLAGS)
1596                 inode->i_flags = ll_ext_to_inode_flags(body->flags);
1597         if (body->valid & OBD_MD_FLNLINK)
1598                 inode->i_nlink = body->nlink;
1599         if (body->valid & OBD_MD_FLGENER)
1600                 inode->i_generation = body->generation;
1601         if (body->valid & OBD_MD_FLRDEV)
1602 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1603                 inode->i_rdev = body->rdev;
1604 #else
1605                 inode->i_rdev = old_decode_dev(body->rdev);
1606 #endif
1607         if (body->valid & OBD_MD_FLSIZE)
1608                 inode->i_size = body->size;
1609         if (body->valid & OBD_MD_FLBLOCKS)
1610                 inode->i_blocks = body->blocks;
1611
1612         if (body->valid & OBD_MD_FLSIZE)
1613                 set_bit(LLI_F_HAVE_MDS_SIZE_LOCK, &lli->lli_flags);
1614 }
1615
1616 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
1617 static struct backing_dev_info ll_backing_dev_info = {
1618         .ra_pages       = 0,    /* No readahead */
1619 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12))
1620         .capabilities   = 0,    /* Does contribute to dirty memory */
1621 #else
1622         .memory_backed  = 0,    /* Does contribute to dirty memory */
1623 #endif
1624 };
1625 #endif
1626
1627 void ll_read_inode2(struct inode *inode, void *opaque)
1628 {
1629         struct lustre_md *md = opaque;
1630         struct ll_inode_info *lli = ll_i2info(inode);
1631         ENTRY;
1632
1633         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
1634                inode->i_generation, inode);
1635
1636         ll_lli_init(lli);
1637
1638         LASSERT(!lli->lli_smd);
1639
1640         /* Core attributes from the MDS first.  This is a new inode, and
1641          * the VFS doesn't zero times in the core inode so we have to do
1642          * it ourselves.  They will be overwritten by either MDS or OST
1643          * attributes - we just need to make sure they aren't newer. */
1644         LTIME_S(inode->i_mtime) = 0;
1645         LTIME_S(inode->i_atime) = 0;
1646         LTIME_S(inode->i_ctime) = 0;
1647         inode->i_rdev = 0;
1648         ll_update_inode(inode, md);
1649
1650         /* OIDEBUG(inode); */
1651
1652         if (S_ISREG(inode->i_mode)) {
1653                 struct ll_sb_info *sbi = ll_i2sbi(inode);
1654                 inode->i_op = &ll_file_inode_operations;
1655                 inode->i_fop = sbi->ll_fop;
1656                 inode->i_mapping->a_ops = &ll_aops;
1657                 EXIT;
1658         } else if (S_ISDIR(inode->i_mode)) {
1659                 inode->i_op = &ll_dir_inode_operations;
1660                 inode->i_fop = &ll_dir_operations;
1661                 inode->i_mapping->a_ops = &ll_dir_aops;
1662                 EXIT;
1663         } else if (S_ISLNK(inode->i_mode)) {
1664                 inode->i_op = &ll_fast_symlink_inode_operations;
1665                 EXIT;
1666         } else {
1667                 inode->i_op = &ll_special_inode_operations;
1668
1669 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1670                 init_special_inode(inode, inode->i_mode,
1671                                    kdev_t_to_nr(inode->i_rdev));
1672
1673                 /* initializing backing dev info. */
1674                 inode->i_mapping->backing_dev_info = &ll_backing_dev_info;
1675 #else
1676                 init_special_inode(inode, inode->i_mode, inode->i_rdev);
1677 #endif
1678                 EXIT;
1679         }
1680 }
1681
1682 int ll_iocontrol(struct inode *inode, struct file *file,
1683                  unsigned int cmd, unsigned long arg)
1684 {
1685         struct ll_sb_info *sbi = ll_i2sbi(inode);
1686         struct ptlrpc_request *req = NULL;
1687         int rc, flags = 0;
1688         ENTRY;
1689
1690         switch(cmd) {
1691         case EXT3_IOC_GETFLAGS: {
1692                 struct ll_fid fid;
1693                 struct mds_body *body;
1694
1695                 ll_inode2fid(&fid, inode);
1696                 rc = mdc_getattr(sbi->ll_mdc_exp, &fid, OBD_MD_FLFLAGS,0,&req);
1697                 if (rc) {
1698                         CERROR("failure %d inode %lu\n", rc, inode->i_ino);
1699                         RETURN(-abs(rc));
1700                 }
1701
1702                 body = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
1703                                       sizeof(*body));
1704
1705                 /* We want to return EXT3_*_FL flags to the caller via this
1706                  * ioctl.  An older MDS may be sending S_* flags, fix it up. */
1707                 flags = ll_inode_to_ext_flags(body->flags, body->flags);
1708                 ptlrpc_req_finished (req);
1709
1710                 RETURN(put_user(flags, (int *)arg));
1711         }
1712         case EXT3_IOC_SETFLAGS: {
1713                 struct mdc_op_data op_data;
1714                 struct ll_iattr_struct attr;
1715                 struct obd_info oinfo = { { { 0 } } };
1716                 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
1717
1718                 if (get_user(flags, (int *)arg))
1719                         RETURN(-EFAULT);
1720
1721                 oinfo.oi_md = lsm;
1722                 oinfo.oi_oa = obdo_alloc();
1723                 if (!oinfo.oi_oa)
1724                         RETURN(-ENOMEM);
1725
1726                 ll_prepare_mdc_op_data(&op_data, inode, NULL, NULL, 0, 0);
1727
1728                 memset(&attr, 0, sizeof(attr));
1729                 attr.ia_attr_flags = flags;
1730                 ((struct iattr *)&attr)->ia_valid |= ATTR_ATTR_FLAG;
1731
1732                 rc = mdc_setattr(sbi->ll_mdc_exp, &op_data,
1733                                  (struct iattr *)&attr, NULL, 0, NULL, 0, &req);
1734                 ptlrpc_req_finished(req);
1735                 if (rc || lsm == NULL) {
1736                         obdo_free(oinfo.oi_oa);
1737                         RETURN(rc);
1738                 }
1739
1740                 oinfo.oi_oa->o_id = lsm->lsm_object_id;
1741                 oinfo.oi_oa->o_flags = flags;
1742                 oinfo.oi_oa->o_valid = OBD_MD_FLID | OBD_MD_FLFLAGS;
1743
1744                 obdo_from_inode(oinfo.oi_oa, inode,
1745                                 OBD_MD_FLFID | OBD_MD_FLGENER);
1746                 rc = obd_setattr_rqset(sbi->ll_osc_exp, &oinfo, NULL);
1747                 obdo_free(oinfo.oi_oa);
1748                 if (rc) {
1749                         if (rc != -EPERM && rc != -EACCES)
1750                                 CERROR("mdc_setattr_async fails: rc = %d\n", rc);
1751                         RETURN(rc);
1752                 }
1753
1754                 inode->i_flags = ll_ext_to_inode_flags(flags |
1755                                                        MDS_BFLAG_EXT_FLAGS);
1756                 RETURN(0);
1757         }
1758         default:
1759                 RETURN(-ENOSYS);
1760         }
1761
1762         RETURN(0);
1763 }
1764
1765 /* umount -f client means force down, don't save state */
1766 #ifdef HAVE_UMOUNTBEGIN_VFSMOUNT
1767 void ll_umount_begin(struct vfsmount *vfsmnt, int flags)
1768 {
1769         struct super_block *sb = vfsmnt->mnt_sb;
1770 #else
1771 void ll_umount_begin(struct super_block *sb)
1772 {
1773 #endif
1774         struct lustre_sb_info *lsi = s2lsi(sb);
1775         struct ll_sb_info *sbi = ll_s2sbi(sb);
1776         struct obd_device *obd;
1777         struct obd_ioctl_data ioc_data = { 0 };
1778         ENTRY;
1779
1780 #ifdef HAVE_UMOUNTBEGIN_VFSMOUNT
1781         if (!(flags & MNT_FORCE)) {
1782                 EXIT;
1783                 return;
1784         }
1785 #endif
1786
1787         /* Tell the MGC we got umount -f */
1788         lsi->lsi_flags |= LSI_UMOUNT_FORCE;
1789
1790         CDEBUG(D_VFSTRACE, "VFS Op: superblock %p count %d active %d\n", sb,
1791                sb->s_count, atomic_read(&sb->s_active));
1792
1793         obd = class_exp2obd(sbi->ll_mdc_exp);
1794         if (obd == NULL) {
1795                 CERROR("Invalid MDC connection handle "LPX64"\n",
1796                        sbi->ll_mdc_exp->exp_handle.h_cookie);
1797                 EXIT;
1798                 return;
1799         }
1800         obd->obd_no_recov = 1;
1801         obd_iocontrol(IOC_OSC_SET_ACTIVE, sbi->ll_mdc_exp, sizeof ioc_data,
1802                       &ioc_data, NULL);
1803
1804         obd = class_exp2obd(sbi->ll_osc_exp);
1805         if (obd == NULL) {
1806                 CERROR("Invalid LOV connection handle "LPX64"\n",
1807                        sbi->ll_osc_exp->exp_handle.h_cookie);
1808                 EXIT;
1809                 return;
1810         }
1811
1812         obd->obd_no_recov = 1;
1813         obd_iocontrol(IOC_OSC_SET_ACTIVE, sbi->ll_osc_exp, sizeof ioc_data,
1814                       &ioc_data, NULL);
1815
1816         /* Really, we'd like to wait until there are no requests outstanding,
1817          * and then continue.  For now, we just invalidate the requests,
1818          * schedule, and hope.
1819          */
1820         schedule();
1821
1822         EXIT;
1823 }
1824
1825 int ll_remount_fs(struct super_block *sb, int *flags, char *data)
1826 {
1827         struct ll_sb_info *sbi = ll_s2sbi(sb);
1828         int err;
1829         __u32 read_only;
1830  
1831         if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY)) {
1832                 read_only = *flags & MS_RDONLY;
1833                 err = obd_set_info_async(sbi->ll_mdc_exp, strlen("read-only"),
1834                                          "read-only", sizeof(read_only),
1835                                          &read_only, NULL);
1836                 if (err) {
1837                         CERROR("Failed to change the read-only flag during "
1838                                "remount: %d\n", err);
1839                         return err;
1840                 }
1841  
1842                 if (read_only)
1843                         sb->s_flags |= MS_RDONLY;
1844                 else
1845                         sb->s_flags &= ~MS_RDONLY;
1846         }
1847         return 0;
1848 }
1849
1850 int ll_prep_inode(struct obd_export *exp, struct inode **inode,
1851                   struct ptlrpc_request *req, int offset,struct super_block *sb)
1852 {
1853         struct lustre_md md;
1854         struct ll_sb_info *sbi = NULL;
1855         int rc = 0;
1856         ENTRY;
1857
1858         LASSERT(*inode || sb);
1859         sbi = sb ? ll_s2sbi(sb) : ll_i2sbi(*inode);
1860         prune_deathrow(sbi, 1);
1861
1862         rc = mdc_req2lustre_md(req, offset, exp, &md);
1863         if (rc)
1864                 RETURN(rc);
1865
1866         if (*inode) {
1867                 ll_update_inode(*inode, &md);
1868         } else {
1869                 LASSERT(sb);
1870                 *inode = ll_iget(sb, md.body->ino, &md);
1871                 if (*inode == NULL || is_bad_inode(*inode)) {
1872                         mdc_free_lustre_md(exp, &md);
1873                         rc = -ENOMEM;
1874                         CERROR("new_inode -fatal: rc %d\n", rc);
1875                         GOTO(out, rc);
1876                 }
1877         }
1878
1879         rc = obd_checkmd(exp, ll_i2mdcexp(*inode),
1880                          ll_i2info(*inode)->lli_smd);
1881 out:
1882         RETURN(rc);
1883 }
1884
1885 char *llap_origins[] = {
1886         [LLAP_ORIGIN_UNKNOWN] = "--",
1887         [LLAP_ORIGIN_READPAGE] = "rp",
1888         [LLAP_ORIGIN_READAHEAD] = "ra",
1889         [LLAP_ORIGIN_COMMIT_WRITE] = "cw",
1890         [LLAP_ORIGIN_WRITEPAGE] = "wp",
1891 };
1892
1893 struct ll_async_page *llite_pglist_next_llap(struct ll_sb_info *sbi,
1894                                              struct list_head *list)
1895 {
1896         struct ll_async_page *llap;
1897         struct list_head *pos;
1898
1899         list_for_each(pos, list) {
1900                 if (pos == &sbi->ll_pglist)
1901                         return NULL;
1902                 llap = list_entry(pos, struct ll_async_page, llap_pglist_item);
1903                 if (llap->llap_page == NULL)
1904                         continue;
1905                 return llap;
1906         }
1907         LBUG();
1908         return NULL;
1909 }
1910
1911 int ll_obd_statfs(struct inode *inode, void *arg)
1912 {
1913         struct ll_sb_info *sbi = NULL;
1914         struct obd_device *client_obd = NULL, *lov_obd = NULL;
1915         struct lov_obd *lov = NULL;
1916         struct obd_statfs stat_buf = {0};
1917         char *buf = NULL;
1918         struct obd_ioctl_data *data = NULL;
1919         __u32 type, index;
1920         int len = 0, rc;
1921
1922         if (!inode || !(sbi = ll_i2sbi(inode)))
1923                 GOTO(out_statfs, rc = -EINVAL);
1924
1925         rc = obd_ioctl_getdata(&buf, &len, arg);
1926         if (rc)
1927                 GOTO(out_statfs, rc);
1928
1929         data = (void*)buf;
1930         if (!data->ioc_inlbuf1 || !data->ioc_inlbuf2 ||
1931             !data->ioc_pbuf1 || !data->ioc_pbuf2)
1932                 GOTO(out_statfs, rc = -EINVAL);
1933
1934         memcpy(&type, data->ioc_inlbuf1, sizeof(__u32));
1935         memcpy(&index, data->ioc_inlbuf2, sizeof(__u32));
1936
1937         if (type == LL_STATFS_MDC) {
1938                 if (index > 0)
1939                         GOTO(out_statfs, rc = -ENODEV);
1940                 client_obd = class_exp2obd(sbi->ll_mdc_exp);
1941         } else if (type == LL_STATFS_LOV) {
1942                 lov_obd = class_exp2obd(sbi->ll_osc_exp);
1943                 lov = &lov_obd->u.lov;
1944
1945                 if (index >= lov->desc.ld_tgt_count)
1946                         GOTO(out_statfs, rc = -ENODEV);
1947                 
1948                 if (!lov->lov_tgts[index])
1949                         /* Try again with the next index */
1950                         GOTO(out_statfs, rc = -EAGAIN);
1951
1952                 client_obd = class_exp2obd(lov->lov_tgts[index]->ltd_exp);
1953                 if (!lov->lov_tgts[index]->ltd_active)
1954                         GOTO(out_uuid, rc = -ENODATA);
1955         }
1956
1957         if (!client_obd)
1958                 GOTO(out_statfs, rc = -EINVAL);
1959
1960         rc = obd_statfs(client_obd, &stat_buf, cfs_time_current_64() - 1);
1961         if (rc)
1962                 GOTO(out_statfs, rc);
1963
1964         if (copy_to_user(data->ioc_pbuf1, &stat_buf, data->ioc_plen1))
1965                 GOTO(out_statfs, rc = -EFAULT);
1966
1967 out_uuid:
1968         if (copy_to_user(data->ioc_pbuf2, obd2cli_tgt(client_obd),
1969                          data->ioc_plen2))
1970                 rc = -EFAULT;
1971
1972 out_statfs:
1973         if (buf)
1974                 obd_ioctl_freedata(buf, len);
1975         return rc;
1976 }
1977
1978 int ll_process_config(struct lustre_cfg *lcfg)
1979 {
1980         char *ptr;
1981         void *sb;
1982         struct lprocfs_static_vars lvars;
1983         unsigned long x; 
1984         int rc = 0;
1985
1986         lprocfs_init_vars(llite, &lvars);
1987
1988         /* The instance name contains the sb: lustre-client-aacfe000 */
1989         ptr = strrchr(lustre_cfg_string(lcfg, 0), '-');
1990         if (!ptr || !*(++ptr)) 
1991                 return -EINVAL;
1992         if (sscanf(ptr, "%lx", &x) != 1)
1993                 return -EINVAL;
1994         sb = (void *)x;
1995         /* This better be a real Lustre superblock! */
1996         LASSERT(s2lsi((struct super_block *)sb)->lsi_lmd->lmd_magic == LMD_MAGIC);
1997
1998         /* Note we have not called client_common_fill_super yet, so 
1999            proc fns must be able to handle that! */
2000         rc = class_process_proc_param(PARAM_LLITE, lvars.obd_vars,
2001                                       lcfg, sb);
2002         return(rc);
2003 }
2004