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