Whamcloud - gitweb
beed138e8675309f479b02f1f4bb2679cebf344e
[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 <linux/lustre_lite.h>
32 #include <linux/lustre_ha.h>
33 #include <linux/lustre_ver.h>
34 #include <linux/lustre_dlm.h>
35 #include <linux/lprocfs_status.h>
36 #include <linux/lustre_disk.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 /* We need to have some extra twiddling here because some systems have
52  * no random state when they start up. */
53 static void
54 lustre_generate_random_uuid(class_uuid_t uuid)
55 {
56         struct timeval t;
57         int *i, j, k;
58
59         ENTRY;
60         LASSERT(sizeof(class_uuid_t) % sizeof(*i) == 0);
61
62         j = jiffies;
63         do_gettimeofday(&t);
64         k = t.tv_usec;
65
66         generate_random_uuid(uuid);
67
68         for (i = (int *)uuid; (char *)i < (char *)uuid + sizeof(class_uuid_t); i++) {
69                 *i ^= j ^ k;
70                 j = ((j << 8) & 0xffffff00) | ((j >> 24) & 0x000000ff);
71                 k = ((k >> 8) & 0x00ffffff) | ((k << 24) & 0xff000000);
72         }
73
74         EXIT;
75 }
76
77 struct ll_sb_info *ll_init_sbi(void)
78 {
79         struct ll_sb_info *sbi = NULL;
80         class_uuid_t uuid;
81         ENTRY;
82
83         OBD_ALLOC(sbi, sizeof(*sbi));
84         if (!sbi)
85                 RETURN(NULL);
86
87         spin_lock_init(&sbi->ll_lock);
88         spin_lock_init(&sbi->ll_lco.lco_lock);
89         INIT_LIST_HEAD(&sbi->ll_pglist);
90         sbi->ll_pglist_gen = 0;
91         if (num_physpages >> (20 - PAGE_SHIFT) < 512)
92                 sbi->ll_async_page_max = num_physpages / 2;
93         else
94                 sbi->ll_async_page_max = (num_physpages / 4) * 3;
95         sbi->ll_ra_info.ra_max_pages = min(num_physpages / 8,
96                                            SBI_DEFAULT_READAHEAD_MAX);
97
98         INIT_LIST_HEAD(&sbi->ll_conn_chain);
99         INIT_HLIST_HEAD(&sbi->ll_orphan_dentry_list);
100
101         lustre_generate_random_uuid(uuid);
102         class_uuid_unparse(uuid, &sbi->ll_sb_uuid);
103         CDEBUG(D_HA, "generated uuid: %s\n", sbi->ll_sb_uuid.uuid);
104
105         spin_lock(&ll_sb_lock);
106         list_add_tail(&sbi->ll_list, &ll_super_blocks);
107         spin_unlock(&ll_sb_lock);
108
109         INIT_LIST_HEAD(&sbi->ll_deathrow);
110         spin_lock_init(&sbi->ll_deathrow_lock);
111         RETURN(sbi);
112 }
113
114 void ll_free_sbi(struct super_block *sb)
115 {
116         struct ll_sb_info *sbi = ll_s2sbi(sb);
117         ENTRY;
118
119         if (sbi != NULL) {
120                 spin_lock(&ll_sb_lock);
121                 list_del(&sbi->ll_list);
122                 spin_unlock(&ll_sb_lock);
123                 OBD_FREE(sbi, sizeof(*sbi));
124         }
125         EXIT;
126 }
127
128 static struct dentry_operations ll_d_root_ops = {
129         .d_compare = ll_dcompare,
130 };
131
132 int client_common_fill_super(struct super_block *sb, char *mdc, char *osc)
133 {
134         struct inode *root = 0;
135         struct ll_sb_info *sbi = ll_s2sbi(sb);
136         struct obd_device *obd;
137         struct ll_fid rootfid;
138         struct obd_statfs osfs;
139         struct ptlrpc_request *request = NULL;
140         struct lustre_handle osc_conn = {0, };
141         struct lustre_handle mdc_conn = {0, };
142         struct lustre_md md;
143         struct obd_connect_data *data = NULL;
144         int err;
145         ENTRY;
146
147         obd = class_name2obd(mdc);
148         if (!obd) {
149                 CERROR("MDC %s: not setup or attached\n", mdc);
150                 RETURN(-EINVAL);
151         }
152
153         OBD_ALLOC(data, sizeof(*data));
154         if (data == NULL)
155                 RETURN(-ENOMEM);
156
157         if (proc_lustre_fs_root) {
158                 err = lprocfs_register_mountpoint(proc_lustre_fs_root, sb,
159                                                   osc, mdc);
160                 if (err < 0)
161                         CERROR("could not register mount in /proc/lustre");
162         }
163
164         /* indicate that inodebits locking is supported by this client */
165         data->ocd_connect_flags |= OBD_CONNECT_IBITS;
166         data->ocd_ibits_known = MDS_INODELOCK_FULL;
167
168         if (sb->s_flags & MS_RDONLY)
169                 data->ocd_connect_flags |= OBD_CONNECT_RDONLY;
170         if (sbi->ll_flags & LL_SBI_USER_XATTR)
171                 data->ocd_connect_flags |= OBD_CONNECT_XATTR;
172         data->ocd_connect_flags |= OBD_CONNECT_ACL | OBD_CONNECT_JOIN;
173
174         if (sbi->ll_flags & LL_SBI_FLOCK) {
175                 sbi->ll_fop = &ll_file_operations_flock;
176         } else {
177                 sbi->ll_fop = &ll_file_operations;
178         }
179
180         data->ocd_connect_flags |= OBD_CONNECT_VERSION;
181         data->ocd_version = LUSTRE_VERSION_CODE;
182
183         err = obd_connect(&mdc_conn, obd, &sbi->ll_sb_uuid, data);
184         if (err == -EBUSY) {
185                 CERROR("An MDT (mdc %s) is performing recovery, of which this"
186                        " client is not a part.  Please wait for recovery to "
187                        "complete, abort, or time out.\n", mdc);
188                 GOTO(out, err);
189         } else if (err) {
190                 CERROR("cannot connect to %s: rc = %d\n", mdc, err);
191                 GOTO(out, err);
192         }
193         sbi->ll_mdc_exp = class_conn2export(&mdc_conn);
194
195         err = obd_statfs(obd, &osfs, jiffies - HZ);
196         if (err)
197                 GOTO(out_mdc, err);
198
199         /* async connect is surely finished by now */
200         *data = class_exp2cliimp(sbi->ll_mdc_exp)->imp_connect_data;
201
202         LASSERT(osfs.os_bsize);
203         sb->s_blocksize = osfs.os_bsize;
204         sb->s_blocksize_bits = log2(osfs.os_bsize);
205         sb->s_magic = LL_SUPER_MAGIC;
206         sb->s_maxbytes = PAGE_CACHE_MAXBYTES;
207         sbi->ll_namelen = osfs.os_namelen;
208
209         if ((sbi->ll_flags & LL_SBI_USER_XATTR) &&
210             !(data->ocd_connect_flags & OBD_CONNECT_XATTR)) {
211                 LCONSOLE_INFO("Disabling user_xattr feature because "
212                               "it is not supported on the server\n"); 
213                 sbi->ll_flags &= ~LL_SBI_USER_XATTR;
214         }
215
216         if (data->ocd_connect_flags & OBD_CONNECT_ACL) {
217 #ifdef MS_POSIXACL
218                 sb->s_flags |= MS_POSIXACL;
219 #endif
220                 sbi->ll_flags |= LL_SBI_ACL;
221         } else
222                 sbi->ll_flags &= ~LL_SBI_ACL;
223
224         if (data->ocd_connect_flags & OBD_CONNECT_JOIN)
225                 sbi->ll_flags |= LL_SBI_JOIN;
226
227 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0))
228         /* We set sb->s_dev equal on all lustre clients in order to support
229          * NFS export clustering.  NFSD requires that the FSID be the same
230          * on all clients. */
231         /* s_dev is also used in lt_compare() to compare two fs, but that is
232          * only a node-local comparison. */
233         sb->s_dev = get_uuid2int(sbi2mdc(sbi)->cl_import->imp_target_uuid.uuid,
234                          strlen(sbi2mdc(sbi)->cl_import->imp_target_uuid.uuid));
235 #endif
236
237         obd = class_name2obd(osc);
238         if (!obd) {
239                 CERROR("OSC %s: not setup or attached\n", osc);
240                 GOTO(out_mdc, err);
241         }
242
243         data->ocd_connect_flags =
244                 OBD_CONNECT_GRANT|OBD_CONNECT_VERSION|OBD_CONNECT_REQPORTAL;
245
246         CDEBUG(D_RPCTRACE, "ocd_connect_flags: "LPX64" ocd_version: %d "
247                "ocd_grant: %d\n", data->ocd_connect_flags,
248                data->ocd_version, data->ocd_grant);
249
250         obd->obd_upcall.onu_owner = &sbi->ll_lco;
251         obd->obd_upcall.onu_upcall = ll_ocd_update;
252
253         err = obd_connect(&osc_conn, obd, &sbi->ll_sb_uuid, data);
254         if (err == -EBUSY) {
255                 CERROR("An OST (osc %s) is performing recovery, of which this"
256                        " client is not a part.  Please wait for recovery to "
257                        "complete, abort, or time out.\n", osc);
258                 GOTO(out, err);
259         } else if (err) {
260                 CERROR("cannot connect to %s: rc = %d\n", osc, err);
261                 GOTO(out_mdc, err);
262         }
263         sbi->ll_osc_exp = class_conn2export(&osc_conn);
264         spin_lock(&sbi->ll_lco.lco_lock);
265         sbi->ll_lco.lco_flags = data->ocd_connect_flags;
266         spin_unlock(&sbi->ll_lco.lco_lock);
267
268         mdc_init_ea_size(sbi->ll_mdc_exp, sbi->ll_osc_exp);
269
270         if (!ll_async_page_slab) {
271                 ll_async_page_slab_size =
272                         size_round(sizeof(struct ll_async_page)) +
273                         obd_prep_async_page(sbi->ll_osc_exp, NULL, NULL, NULL,
274                                             0, NULL, NULL, NULL);
275                 ll_async_page_slab = kmem_cache_create("ll_async_page",
276                                                        ll_async_page_slab_size,
277                                                        0, 0, NULL, NULL);
278                 if (!ll_async_page_slab)
279                         GOTO(out_osc, -ENOMEM);
280         }
281
282         err = mdc_getstatus(sbi->ll_mdc_exp, &rootfid);
283         if (err) {
284                 CERROR("cannot mds_connect: rc = %d\n", err);
285                 GOTO(out_osc, err);
286         }
287         CDEBUG(D_SUPER, "rootfid "LPU64"\n", rootfid.id);
288         sbi->ll_rootino = rootfid.id;
289
290         sb->s_op = &lustre_super_operations;
291
292         /* make root inode
293          * XXX: move this to after cbd setup? */
294         err = mdc_getattr(sbi->ll_mdc_exp, &rootfid,
295                           OBD_MD_FLGETATTR | OBD_MD_FLBLOCKS |
296                           (sbi->ll_flags & LL_SBI_ACL ? OBD_MD_FLACL : 0),
297                           0, &request);
298         if (err) {
299                 CERROR("mdc_getattr failed for root: rc = %d\n", err);
300                 GOTO(out_osc, err);
301         }
302
303         err = mdc_req2lustre_md(request, 0, sbi->ll_osc_exp, &md);
304         if (err) {
305                 CERROR("failed to understand root inode md: rc = %d\n",err);
306                 ptlrpc_req_finished (request);
307                 GOTO(out_osc, err);
308         }
309
310         LASSERT(sbi->ll_rootino != 0);
311         root = ll_iget(sb, sbi->ll_rootino, &md);
312
313         ptlrpc_req_finished(request);
314
315         if (root == NULL || is_bad_inode(root)) {
316                 mdc_free_lustre_md(sbi->ll_osc_exp, &md);
317                 CERROR("lustre_lite: bad iget4 for root\n");
318                 GOTO(out_root, err = -EBADF);
319         }
320
321         err = ll_close_thread_start(&sbi->ll_lcq);
322         if (err) {
323                 CERROR("cannot start close thread: rc %d\n", err);
324                 GOTO(out_root, err);
325         }
326
327         /* making vm readahead 0 for 2.4.x. In the case of 2.6.x,
328            backing dev info assigned to inode mapping is used for
329            determining maximal readahead. */
330 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)) && \
331     !defined(KERNEL_HAS_AS_MAX_READAHEAD)
332         /* bug 2805 - set VM readahead to zero */
333         vm_max_readahead = vm_min_readahead = 0;
334 #endif
335
336         sb->s_root = d_alloc_root(root);
337         if (data != NULL)
338                 OBD_FREE(data, sizeof(*data));
339         sb->s_root->d_op = &ll_d_root_ops;
340         RETURN(err);
341
342 out_root:
343         if (root)
344                 iput(root);
345 out_osc:
346         obd_disconnect(sbi->ll_osc_exp);
347 out_mdc:
348         obd_disconnect(sbi->ll_mdc_exp);
349 out:
350         if (data != NULL)
351                 OBD_FREE(data, sizeof(*data));
352         lprocfs_unregister_mountpoint(sbi);
353         RETURN(err);
354 }
355
356 int ll_get_max_mdsize(struct ll_sb_info *sbi, int *lmmsize)
357 {
358         int size, rc;
359
360         *lmmsize = obd_size_diskmd(sbi->ll_osc_exp, NULL);
361         size = sizeof(int);
362         rc = obd_get_info(sbi->ll_mdc_exp, strlen("max_easize"), "max_easize", 
363                           &size, lmmsize);
364         if (rc) 
365                 CERROR("Get max mdsize error rc %d \n", rc);
366         
367         RETURN(rc);
368 }
369
370 void ll_dump_inode(struct inode *inode)
371 {
372         struct list_head *tmp;
373         int dentry_count = 0;
374
375         LASSERT(inode != NULL);
376
377         list_for_each(tmp, &inode->i_dentry)
378                 dentry_count++;
379
380         CERROR("inode %p dump: dev=%s ino=%lu mode=%o count=%u, %d dentries\n",
381                inode, ll_i2mdcexp(inode)->exp_obd->obd_name, inode->i_ino,
382                inode->i_mode, atomic_read(&inode->i_count), dentry_count);
383 }
384
385 void lustre_dump_dentry(struct dentry *dentry, int recur)
386 {
387         struct list_head *tmp;
388         int subdirs = 0;
389
390         LASSERT(dentry != NULL);
391
392         list_for_each(tmp, &dentry->d_subdirs)
393                 subdirs++;
394
395         CERROR("dentry %p dump: name=%.*s parent=%.*s (%p), inode=%p, count=%u,"
396                " flags=0x%x, fsdata=%p, %d subdirs\n", dentry,
397                dentry->d_name.len, dentry->d_name.name,
398                dentry->d_parent->d_name.len, dentry->d_parent->d_name.name,
399                dentry->d_parent, dentry->d_inode, atomic_read(&dentry->d_count),
400                dentry->d_flags, dentry->d_fsdata, subdirs);
401         if (dentry->d_inode != NULL)
402                 ll_dump_inode(dentry->d_inode);
403
404         if (recur == 0)
405                 return;
406
407         list_for_each(tmp, &dentry->d_subdirs) {
408                 struct dentry *d = list_entry(tmp, struct dentry, d_child);
409                 lustre_dump_dentry(d, recur - 1);
410         }
411 }
412
413 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
414 void lustre_throw_orphan_dentries(struct super_block *sb)
415 {
416         struct hlist_node *tmp, *next;
417         struct ll_sb_info *sbi = ll_s2sbi(sb);
418
419         /* Do this to get rid of orphaned dentries. That is not really trw. */
420         hlist_for_each_safe(tmp, next, &sbi->ll_orphan_dentry_list) {
421                 struct dentry *dentry = hlist_entry(tmp, struct dentry, d_hash);
422                 CWARN("found orphan dentry %.*s (%p->%p) at unmount, dumping "
423                       "before and after shrink_dcache_parent\n",
424                       dentry->d_name.len, dentry->d_name.name, dentry, next);
425                 lustre_dump_dentry(dentry, 1);
426                 shrink_dcache_parent(dentry);
427                 lustre_dump_dentry(dentry, 1);
428         }
429 }
430 #else
431 #define lustre_throw_orphan_dentries(sb)
432 #endif
433
434 static void prune_deathrow(struct ll_sb_info *sbi, int try)
435 {
436         LIST_HEAD(throw_away);
437         int locked = 0;
438         ENTRY;
439
440         if (try) {
441                 locked = spin_trylock(&sbi->ll_deathrow_lock);
442         } else {
443                 spin_lock(&sbi->ll_deathrow_lock);
444                 locked = 1;
445         }
446
447         if (!locked) {
448                 EXIT;
449                 return;
450         }
451
452         list_splice_init(&sbi->ll_deathrow, &throw_away);
453         spin_unlock(&sbi->ll_deathrow_lock);
454
455         while (!list_empty(&throw_away)) {
456                 struct ll_inode_info *lli;
457                 struct inode *inode;
458
459                 lli = list_entry(throw_away.next, struct ll_inode_info,
460                                  lli_dead_list);
461                 list_del_init(&lli->lli_dead_list);
462
463                 inode = ll_info2i(lli);
464                 d_prune_aliases(inode);
465
466                 CDEBUG(D_INODE, "prune duplicate inode %p inum %lu count %u\n",
467                        inode, inode->i_ino, atomic_read(&inode->i_count));
468                 iput(inode);
469         }
470         EXIT;
471 }
472
473 void client_common_put_super(struct super_block *sb)
474 {
475         struct ll_sb_info *sbi = ll_s2sbi(sb);
476         ENTRY;
477
478         ll_close_thread_shutdown(sbi->ll_lcq);
479
480         /* destroy inodes in deathrow */
481         prune_deathrow(sbi, 0);
482
483         list_del(&sbi->ll_conn_chain);
484         obd_disconnect(sbi->ll_osc_exp);
485
486         lprocfs_unregister_mountpoint(sbi);
487         if (sbi->ll_proc_root) {
488                 lprocfs_remove(sbi->ll_proc_root);
489                 sbi->ll_proc_root = NULL;
490         }
491
492         obd_disconnect(sbi->ll_mdc_exp);
493
494         lustre_throw_orphan_dentries(sb);
495
496         EXIT;
497 }
498
499 char *ll_read_opt(const char *opt, char *data)
500 {
501         char *value;
502         char *retval;
503         ENTRY;
504
505         CDEBUG(D_SUPER, "option: %s, data %s\n", opt, data);
506         if (strncmp(opt, data, strlen(opt)))
507                 RETURN(NULL);
508         if ((value = strchr(data, '=')) == NULL)
509                 RETURN(NULL);
510
511         value++;
512         OBD_ALLOC(retval, strlen(value) + 1);
513         if (!retval) {
514                 CERROR("out of memory!\n");
515                 RETURN(NULL);
516         }
517
518         memcpy(retval, value, strlen(value)+1);
519         CDEBUG(D_SUPER, "Assigned option: %s, value %s\n", opt, retval);
520         RETURN(retval);
521 }
522
523 int ll_set_opt(const char *opt, char *data, int fl)
524 {
525         ENTRY;
526
527         CDEBUG(D_SUPER, "option: %s, data %s\n", opt, data);
528         if (strncmp(opt, data, strlen(opt)) != 0)
529                 RETURN(0);
530         else
531                 RETURN(fl);
532 }
533
534 /* non-client-specific mount options are parsed in lmd_parse */
535 void ll_options(char *options, int *flags)
536 {
537         int tmp;
538         char *this_char;
539 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
540         char *opt_ptr = options;
541 #endif
542         ENTRY;
543
544         if (!options) {
545                 EXIT;
546                 return;
547         }
548
549         CDEBUG(D_CONFIG, "Parsing opts %s\n", options);
550 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
551         for (this_char = strtok (options, ",");
552              this_char != NULL;
553              this_char = strtok (NULL, ","))
554 #else
555         while ((this_char = strsep (&opt_ptr, ",")) != NULL)
556 #endif
557         {
558                 CDEBUG(D_SUPER, "this_char %s\n", this_char);
559                 tmp = ll_set_opt("nolock", this_char, LL_SBI_NOLCK);
560                 if (tmp) {
561                         *flags |= tmp;
562                         continue;
563                 }
564                 tmp = ll_set_opt("flock", this_char, LL_SBI_FLOCK);
565                 if (tmp) {
566                         *flags |= tmp;
567                         continue;
568                 }
569                 tmp = ll_set_opt("noflock", this_char, LL_SBI_FLOCK);
570                 if (tmp) {
571                         *flags &= ~tmp;
572                         continue;
573                 }
574                 tmp = ll_set_opt("user_xattr", this_char, LL_SBI_USER_XATTR);
575                 if (tmp) {
576                         *flags |= tmp;
577                         continue;
578                 }
579                 tmp = ll_set_opt("nouser_xattr", this_char, LL_SBI_USER_XATTR);
580                 if (tmp) {
581                         *flags &= ~tmp;
582                         continue;
583                 }
584                 tmp = ll_set_opt("acl", this_char, LL_SBI_ACL);
585                 if (tmp) {
586                         /* Ignore deprecated mount option.  The client will
587                          * always try to mount with ACL support, whether this
588                          * is used depends on whether server supports it. */
589                         continue;
590                 }
591                 tmp = ll_set_opt("noacl", this_char, LL_SBI_ACL);
592                 if (tmp) {
593                         continue;
594                 }
595         }
596         CERROR("flags %#x\n", *flags);
597
598         EXIT;
599 }
600                 
601 void ll_lli_init(struct ll_inode_info *lli)
602 {
603         sema_init(&lli->lli_open_sem, 1);
604         sema_init(&lli->lli_size_sem, 1);
605         lli->lli_flags = 0;
606         lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
607         spin_lock_init(&lli->lli_lock);
608         INIT_LIST_HEAD(&lli->lli_pending_write_llaps);
609         lli->lli_inode_magic = LLI_INODE_MAGIC;
610         INIT_LIST_HEAD(&lli->lli_dead_list);
611 }
612
613 int ll_fill_super(struct super_block *sb)
614 {
615         struct lustre_profile *lprof;
616         struct lustre_sb_info *lsi = s2lsi(sb);
617         struct ll_sb_info *sbi;
618         char  *osc = NULL;
619         char  *mdc = NULL;
620         char  *profilenm = get_profile_name(sb);
621         struct config_llog_instance cfg;
622         char   ll_instance[sizeof(sb) * 2 + 1];
623         int    err;
624         ENTRY;
625                                                                                  
626         CDEBUG(D_VFSTRACE, "VFS Op: sb %p\n", sb);
627
628         /* client additional sb info */
629         lsi->lsi_llsbi = sbi = ll_init_sbi();
630         if (!sbi) 
631                 RETURN(-ENOMEM);
632
633         ll_options(lsi->lsi_lmd->lmd_opts, &sbi->ll_flags);
634         
635         /* Generate a string unique to this super, in case some joker tries
636            to mount the same fs at two mount points. 
637            Use the address of the super itself.*/
638         sprintf(ll_instance, "%p", sb);
639         cfg.cfg_instance = ll_instance;
640         cfg.cfg_uuid = lsi->lsi_llsbi->ll_sb_uuid;
641         cfg.cfg_last_idx = 0;
642
643         /* set up client obds */
644         err = lustre_process_log(sb, profilenm, &cfg);
645         if (err < 0) {
646                 CERROR("Unable to process log: %d\n", err);
647                 GOTO(out_free, err);
648         }
649
650         lprof = class_get_profile(profilenm);
651         if (lprof == NULL) {
652                 CERROR("No profile found: %s\n", profilenm);
653                 GOTO(out_free, err = -EINVAL);
654         }
655         CDEBUG(D_CONFIG, "Found profile %s: mdc=%s osc=%s\n", profilenm, 
656                lprof->lp_mdc, lprof->lp_osc);
657
658         OBD_ALLOC(osc, strlen(lprof->lp_osc) +
659                   strlen(ll_instance) + 2);
660         if (!osc) 
661                 GOTO(out_free, err = -ENOMEM);
662         sprintf(osc, "%s-%s", lprof->lp_osc, ll_instance);
663
664         OBD_ALLOC(mdc, strlen(lprof->lp_mdc) +
665                   strlen(ll_instance) + 2);
666         if (!mdc) 
667                 GOTO(out_free, err = -ENOMEM);
668         sprintf(mdc, "%s-%s", lprof->lp_mdc, ll_instance);
669   
670         /* connections, registrations, sb setup */
671         err = client_common_fill_super(sb, mdc, osc);
672   
673 out_free:
674         if (mdc)
675                 OBD_FREE(mdc, strlen(mdc) + 1);
676         if (osc)
677                 OBD_FREE(osc, strlen(osc) + 1);
678         if (err) {
679                 struct obd_device *obd;
680                 int next = 0;
681                 /* like client_put_super below */
682                 while ((obd = class_devices_in_group(&sbi->ll_sb_uuid, &next)) 
683                        != NULL) {
684                         class_manual_cleanup(obd);
685                 }                       
686                 class_del_profile(profilenm);
687                 ll_free_sbi(sb);
688                 lsi->lsi_llsbi = NULL;
689         }
690         RETURN(err);
691 } /* ll_fill_super */
692
693
694 void ll_put_super(struct super_block *sb)
695 {
696         struct config_llog_instance cfg;
697         char   ll_instance[sizeof(sb) * 2 + 1];
698         struct obd_device *obd;
699         struct lustre_sb_info *lsi = s2lsi(sb);
700         struct ll_sb_info *sbi = ll_s2sbi(sb);
701         char *profilenm = get_profile_name(sb);
702         int next = 0;
703         ENTRY;
704
705         CDEBUG(D_VFSTRACE, "VFS Op: sb %p - %s\n", sb, profilenm);
706         
707         sprintf(ll_instance, "%p", sb);
708         cfg.cfg_instance = ll_instance;
709         lustre_end_log(sb, NULL, &cfg);
710         
711         obd = class_exp2obd(sbi->ll_mdc_exp);
712         if (obd) {
713                 int next = 0;
714                 int force = obd->obd_no_recov;
715                 /* We need to set force before the lov_disconnect in 
716                 lustre_common_put_super, since l_d cleans up osc's as well. */
717                 while ((obd = class_devices_in_group(&sbi->ll_sb_uuid, &next)) 
718                        != NULL) {
719                         obd->obd_force = force;
720                 }                       
721         }
722
723         client_common_put_super(sb);
724                 
725         while ((obd = class_devices_in_group(&sbi->ll_sb_uuid, &next)) !=NULL) {
726                 class_manual_cleanup(obd);
727         }                       
728         
729         if (profilenm) 
730                 class_del_profile(profilenm);
731
732         ll_free_sbi(sb);
733         lsi->lsi_llsbi = NULL;
734
735         lustre_common_put_super(sb);
736
737         EXIT;
738 } /* client_put_super */
739
740 #ifdef HAVE_REGISTER_CACHE
741 #include <linux/cache_def.h>
742 #ifdef HAVE_CACHE_RETURN_INT
743 static int
744 #else
745 static void
746 #endif
747 ll_shrink_cache(int priority, unsigned int gfp_mask)
748 {
749         struct ll_sb_info *sbi;
750         int count = 0;
751
752         list_for_each_entry(sbi, &ll_super_blocks, ll_list)
753                 count += llap_shrink_cache(sbi, priority);
754
755 #ifdef HAVE_CACHE_RETURN_INT
756         return count;
757 #endif
758 }
759
760 struct cache_definition ll_cache_definition = {
761         .name = "llap_cache",
762         .shrink = ll_shrink_cache
763 };
764 #endif /* HAVE_REGISTER_CACHE */
765
766 struct inode *ll_inode_from_lock(struct ldlm_lock *lock)
767 {
768         struct inode *inode = NULL;
769         l_lock(&lock->l_resource->lr_namespace->ns_lock);
770         if (lock->l_ast_data) {
771                 struct ll_inode_info *lli = ll_i2info(lock->l_ast_data);
772                 if (lli->lli_inode_magic == LLI_INODE_MAGIC) {
773                         inode = igrab(lock->l_ast_data);
774                 } else {
775                         inode = lock->l_ast_data;
776                         __LDLM_DEBUG(inode->i_state & I_FREEING ?
777                                      D_INFO : D_WARNING, lock,
778                                      "l_ast_data %p is bogus: magic %08x",
779                                      lock->l_ast_data, lli->lli_inode_magic);
780                         inode = NULL;
781                 }
782         }
783         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
784         return inode;
785 }
786
787 static int null_if_equal(struct ldlm_lock *lock, void *data)
788 {
789         if (data == lock->l_ast_data) {
790                 lock->l_ast_data = NULL;
791
792                 if (lock->l_req_mode != lock->l_granted_mode)
793                         LDLM_ERROR(lock,"clearing inode with ungranted lock");
794         }
795
796         return LDLM_ITER_CONTINUE;
797 }
798
799 void ll_clear_inode(struct inode *inode)
800 {
801         struct ll_fid fid;
802         struct ll_inode_info *lli = ll_i2info(inode);
803         struct ll_sb_info *sbi = ll_i2sbi(inode);
804         ENTRY;
805
806         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
807                inode->i_generation, inode);
808
809         ll_inode2fid(&fid, inode);
810         clear_bit(LLI_F_HAVE_MDS_SIZE_LOCK, &(ll_i2info(inode)->lli_flags));
811         mdc_change_cbdata(sbi->ll_mdc_exp, &fid, null_if_equal, inode);
812
813         if (lli->lli_smd) {
814                 obd_change_cbdata(sbi->ll_osc_exp, lli->lli_smd,
815                                   null_if_equal, inode);
816
817                 obd_free_memmd(sbi->ll_osc_exp, &lli->lli_smd);
818                 lli->lli_smd = NULL;
819         }
820
821         if (lli->lli_symlink_name) {
822                 OBD_FREE(lli->lli_symlink_name,
823                          strlen(lli->lli_symlink_name) + 1);
824                 lli->lli_symlink_name = NULL;
825         }
826
827 #ifdef CONFIG_FS_POSIX_ACL
828         if (lli->lli_posix_acl) {
829                 LASSERT(atomic_read(&lli->lli_posix_acl->a_refcount) == 1);
830                 posix_acl_release(lli->lli_posix_acl);
831                 lli->lli_posix_acl = NULL;
832         }
833 #endif
834
835         lli->lli_inode_magic = LLI_INODE_DEAD;
836
837         spin_lock(&sbi->ll_deathrow_lock);
838         list_del_init(&lli->lli_dead_list);
839         spin_unlock(&sbi->ll_deathrow_lock);
840
841         EXIT;
842 }
843
844 /* If this inode has objects allocated to it (lsm != NULL), then the OST
845  * object(s) determine the file size and mtime.  Otherwise, the MDS will
846  * keep these values until such a time that objects are allocated for it.
847  * We do the MDS operations first, as it is checking permissions for us.
848  * We don't to the MDS RPC if there is nothing that we want to store there,
849  * otherwise there is no harm in updating mtime/atime on the MDS if we are
850  * going to do an RPC anyways.
851  *
852  * If we are doing a truncate, we will send the mtime and ctime updates
853  * to the OST with the punch RPC, otherwise we do an explicit setattr RPC.
854  * I don't believe it is possible to get e.g. ATTR_MTIME_SET and ATTR_SIZE
855  * at the same time.
856  */
857 int ll_setattr_raw(struct inode *inode, struct iattr *attr)
858 {
859         struct ll_inode_info *lli = ll_i2info(inode);
860         struct lov_stripe_md *lsm = lli->lli_smd;
861         struct ll_sb_info *sbi = ll_i2sbi(inode);
862         struct ptlrpc_request *request = NULL;
863         struct mdc_op_data op_data;
864         int ia_valid = attr->ia_valid;
865         int rc = 0;
866         ENTRY;
867
868         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu valid %x\n", inode->i_ino,
869                attr->ia_valid);
870         lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_SETATTR);
871
872         if (ia_valid & ATTR_SIZE) {
873                 if (attr->ia_size > ll_file_maxbytes(inode)) {
874                         CDEBUG(D_INODE, "file too large %llu > "LPU64"\n",
875                                attr->ia_size, ll_file_maxbytes(inode));
876                         RETURN(-EFBIG);
877                 }
878
879                 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
880         }
881
882         /* POSIX: check before ATTR_*TIME_SET set (from inode_change_ok) */
883         if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET)) {
884                 if (current->fsuid != inode->i_uid && !capable(CAP_FOWNER))
885                         RETURN(-EPERM);
886         }
887
888         /* We mark all of the fields "set" so MDS/OST does not re-set them */
889         if (attr->ia_valid & ATTR_CTIME) {
890                 attr->ia_ctime = CURRENT_TIME;
891                 attr->ia_valid |= ATTR_CTIME_SET;
892         }
893         if (!(ia_valid & ATTR_ATIME_SET) && (attr->ia_valid & ATTR_ATIME)) {
894                 attr->ia_atime = CURRENT_TIME;
895                 attr->ia_valid |= ATTR_ATIME_SET;
896         }
897         if (!(ia_valid & ATTR_MTIME_SET) && (attr->ia_valid & ATTR_MTIME)) {
898                 attr->ia_mtime = CURRENT_TIME;
899                 attr->ia_valid |= ATTR_MTIME_SET;
900         }
901
902         if (attr->ia_valid & (ATTR_MTIME | ATTR_CTIME))
903                 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu, now = %lu\n",
904                        LTIME_S(attr->ia_mtime), LTIME_S(attr->ia_ctime),
905                        CURRENT_SECONDS);
906
907
908         /* NB: ATTR_SIZE will only be set after this point if the size
909          * resides on the MDS, ie, this file has no objects. */
910         if (lsm)
911                 attr->ia_valid &= ~ATTR_SIZE;
912
913         /* If only OST attributes being set on objects, don't do MDS RPC.
914          * In that case, we need to check permissions and update the local
915          * inode ourselves so we can call obdo_from_inode() always. */
916         if (ia_valid & (lsm ? ~(ATTR_SIZE | ATTR_FROM_OPEN | ATTR_RAW) : ~0)) {
917                 struct lustre_md md;
918                 ll_prepare_mdc_op_data(&op_data, inode, NULL, NULL, 0, 0);
919
920                 rc = mdc_setattr(sbi->ll_mdc_exp, &op_data,
921                                  attr, NULL, 0, NULL, 0, &request);
922
923                 if (rc) {
924                         ptlrpc_req_finished(request);
925                         if (rc != -EPERM && rc != -EACCES)
926                                 CERROR("mdc_setattr fails: rc = %d\n", rc);
927                         RETURN(rc);
928                 }
929
930                 rc = mdc_req2lustre_md(request, 0, sbi->ll_osc_exp, &md);
931                 if (rc) {
932                         ptlrpc_req_finished(request);
933                         RETURN(rc);
934                 }
935
936                 /* We call inode_setattr to adjust timestamps.
937                  * If there is at least some data in file, we cleared ATTR_SIZE
938                  * above to avoid invoking vmtruncate, otherwise it is important
939                  * to call vmtruncate in inode_setattr to update inode->i_size
940                  * (bug 6196) */
941                 rc = inode_setattr(inode, attr);
942
943                 ll_update_inode(inode, &md);
944                 ptlrpc_req_finished(request);
945
946                 if (!lsm || !S_ISREG(inode->i_mode)) {
947                         CDEBUG(D_INODE, "no lsm: not setting attrs on OST\n");
948                         RETURN(rc);
949                 }
950         } else {
951                 /* The OST doesn't check permissions, but the alternative is
952                  * a gratuitous RPC to the MDS.  We already rely on the client
953                  * to do read/write/truncate permission checks, so is mtime OK?
954                  */
955                 if (ia_valid & (ATTR_MTIME | ATTR_ATIME)) {
956                         /* from sys_utime() */
957                         if (!(ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))) {
958                                 if (current->fsuid != inode->i_uid &&
959                                     (rc=ll_permission(inode,MAY_WRITE,NULL))!=0)
960                                         RETURN(rc);
961                         } else {
962                                 /* from inode_change_ok() */
963                                 if (current->fsuid != inode->i_uid &&
964                                     !capable(CAP_FOWNER))
965                                         RETURN(-EPERM);
966                         }
967                 }
968
969                 /* Won't invoke vmtruncate, as we already cleared ATTR_SIZE */
970                 rc = inode_setattr(inode, attr);
971         }
972
973         /* We really need to get our PW lock before we change inode->i_size.
974          * If we don't we can race with other i_size updaters on our node, like
975          * ll_file_read.  We can also race with i_size propogation to other
976          * nodes through dirtying and writeback of final cached pages.  This
977          * last one is especially bad for racing o_append users on other
978          * nodes. */
979         if (ia_valid & ATTR_SIZE) {
980                 ldlm_policy_data_t policy = { .l_extent = {attr->ia_size,
981                                                            OBD_OBJECT_EOF } };
982                 struct lustre_handle lockh = { 0 };
983                 int err, ast_flags = 0;
984                 /* XXX when we fix the AST intents to pass the discard-range
985                  * XXX extent, make ast_flags always LDLM_AST_DISCARD_DATA
986                  * XXX here. */
987                 if (attr->ia_size == 0)
988                         ast_flags = LDLM_AST_DISCARD_DATA;
989
990                 up(&inode->i_sem);
991                 UP_WRITE_I_ALLOC_SEM(inode);
992                 rc = ll_extent_lock(NULL, inode, lsm, LCK_PW, &policy, &lockh,
993                                     ast_flags);
994 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
995                 DOWN_WRITE_I_ALLOC_SEM(inode);
996                 down(&inode->i_sem);
997 #else
998                 down(&inode->i_sem);
999                 DOWN_WRITE_I_ALLOC_SEM(inode);
1000 #endif
1001                 if (rc != 0)
1002                         RETURN(rc);
1003
1004                 /* Only ll_inode_size_lock is taken at this level.
1005                  * lov_stripe_lock() is grabbed by ll_truncate() only over
1006                  * call to obd_adjust_kms().  If vmtruncate returns 0, then
1007                  * ll_truncate dropped ll_inode_size_lock() */
1008                 ll_inode_size_lock(inode, 0);
1009                 rc = vmtruncate(inode, attr->ia_size);
1010                 if (rc != 0) {
1011                         LASSERT(atomic_read(&lli->lli_size_sem.count) <= 0);
1012                         ll_inode_size_unlock(inode, 0);
1013                 }
1014
1015                 err = ll_extent_unlock(NULL, inode, lsm, LCK_PW, &lockh);
1016                 if (err) {
1017                         CERROR("ll_extent_unlock failed: %d\n", err);
1018                         if (!rc)
1019                                 rc = err;
1020                 }
1021         } else if (ia_valid & (ATTR_MTIME | ATTR_MTIME_SET)) {
1022                 obd_flag flags;
1023                 struct obdo oa;
1024
1025                 CDEBUG(D_INODE, "set mtime on OST inode %lu to %lu\n",
1026                        inode->i_ino, LTIME_S(attr->ia_mtime));
1027                 
1028                 oa.o_id = lsm->lsm_object_id;
1029                 oa.o_valid = OBD_MD_FLID;
1030
1031                 flags = OBD_MD_FLTYPE | OBD_MD_FLATIME |
1032                         OBD_MD_FLMTIME | OBD_MD_FLCTIME |
1033                         OBD_MD_FLFID | OBD_MD_FLGENER;
1034                 
1035                 obdo_from_inode(&oa, inode, flags);
1036                 rc = obd_setattr(sbi->ll_osc_exp, &oa, lsm, NULL);
1037                 if (rc)
1038                         CERROR("obd_setattr fails: rc=%d\n", rc);
1039         }
1040         RETURN(rc);
1041 }
1042
1043 int ll_setattr(struct dentry *de, struct iattr *attr)
1044 {
1045         LBUG(); /* code is unused, but leave this in case of VFS changes */
1046         RETURN(-ENOSYS);
1047 }
1048
1049 int ll_statfs_internal(struct super_block *sb, struct obd_statfs *osfs,
1050                        unsigned long max_age)
1051 {
1052         struct ll_sb_info *sbi = ll_s2sbi(sb);
1053         struct obd_statfs obd_osfs;
1054         int rc;
1055         ENTRY;
1056
1057         rc = obd_statfs(class_exp2obd(sbi->ll_mdc_exp), osfs, max_age);
1058         if (rc) {
1059                 CERROR("mdc_statfs fails: rc = %d\n", rc);
1060                 RETURN(rc);
1061         }
1062
1063         osfs->os_type = sb->s_magic;
1064
1065         CDEBUG(D_SUPER, "MDC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1066                osfs->os_bavail, osfs->os_blocks, osfs->os_ffree,osfs->os_files);
1067
1068         rc = obd_statfs(class_exp2obd(sbi->ll_osc_exp), &obd_osfs, max_age);
1069         if (rc) {
1070                 CERROR("obd_statfs fails: rc = %d\n", rc);
1071                 RETURN(rc);
1072         }
1073
1074         CDEBUG(D_SUPER, "OSC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1075                obd_osfs.os_bavail, obd_osfs.os_blocks, obd_osfs.os_ffree,
1076                obd_osfs.os_files);
1077
1078         osfs->os_blocks = obd_osfs.os_blocks;
1079         osfs->os_bfree = obd_osfs.os_bfree;
1080         osfs->os_bavail = obd_osfs.os_bavail;
1081
1082         /* If we don't have as many objects free on the OST as inodes
1083          * on the MDS, we reduce the total number of inodes to
1084          * compensate, so that the "inodes in use" number is correct.
1085          */
1086         if (obd_osfs.os_ffree < osfs->os_ffree) {
1087                 osfs->os_files = (osfs->os_files - osfs->os_ffree) +
1088                         obd_osfs.os_ffree;
1089                 osfs->os_ffree = obd_osfs.os_ffree;
1090         }
1091
1092         RETURN(rc);
1093 }
1094
1095 int ll_statfs(struct super_block *sb, struct kstatfs *sfs)
1096 {
1097         struct obd_statfs osfs;
1098         int rc;
1099
1100         CDEBUG(D_VFSTRACE, "VFS Op:\n");
1101         lprocfs_counter_incr(ll_s2sbi(sb)->ll_stats, LPROC_LL_STAFS);
1102
1103         /* For now we will always get up-to-date statfs values, but in the
1104          * future we may allow some amount of caching on the client (e.g.
1105          * from QOS or lprocfs updates). */
1106         rc = ll_statfs_internal(sb, &osfs, jiffies - 1);
1107         if (rc)
1108                 return rc;
1109
1110         statfs_unpack(sfs, &osfs);
1111
1112         if (sizeof(sfs->f_blocks) == 4) {
1113                 while (osfs.os_blocks > ~0UL) {
1114                         sfs->f_bsize <<= 1;
1115
1116                         osfs.os_blocks >>= 1;
1117                         osfs.os_bfree >>= 1;
1118                         osfs.os_bavail >>= 1;
1119                 }
1120         }
1121
1122         sfs->f_blocks = osfs.os_blocks;
1123         sfs->f_bfree = osfs.os_bfree;
1124         sfs->f_bavail = osfs.os_bavail;
1125
1126         return 0;
1127 }
1128
1129 void ll_inode_size_lock(struct inode *inode, int lock_lsm)
1130 {
1131         struct ll_inode_info *lli;
1132         struct lov_stripe_md *lsm;
1133
1134         lli = ll_i2info(inode);
1135         LASSERT(lli->lli_size_sem_owner != current);
1136         down(&lli->lli_size_sem);
1137         LASSERT(lli->lli_size_sem_owner == NULL);
1138         lli->lli_size_sem_owner = current;
1139         lsm = lli->lli_smd;
1140         LASSERTF(lsm != NULL || lock_lsm == 0, "lsm %p, lock_lsm %d\n",
1141                  lsm, lock_lsm);
1142         if (lock_lsm)
1143                 lov_stripe_lock(lsm);
1144 }
1145
1146 void ll_inode_size_unlock(struct inode *inode, int unlock_lsm)
1147 {
1148         struct ll_inode_info *lli;
1149         struct lov_stripe_md *lsm;
1150
1151         lli = ll_i2info(inode);
1152         lsm = lli->lli_smd;
1153         LASSERTF(lsm != NULL || unlock_lsm == 0, "lsm %p, lock_lsm %d\n",
1154                  lsm, unlock_lsm);
1155         if (unlock_lsm)
1156                 lov_stripe_unlock(lsm);
1157         LASSERT(lli->lli_size_sem_owner == current);
1158         lli->lli_size_sem_owner = NULL;
1159         up(&lli->lli_size_sem);
1160 }
1161
1162 static void ll_replace_lsm(struct inode *inode, struct lov_stripe_md *lsm)
1163 {
1164         struct ll_inode_info *lli = ll_i2info(inode);
1165  
1166         dump_lsm(D_INODE, lsm);
1167         dump_lsm(D_INODE, lli->lli_smd); 
1168         LASSERTF(lsm->lsm_magic == LOV_MAGIC_JOIN, 
1169                  "lsm must be joined lsm %p\n", lsm);
1170         obd_free_memmd(ll_i2obdexp(inode), &lli->lli_smd);
1171         CDEBUG(D_INODE, "replace lsm %p to lli_smd %p for inode %lu%u(%p)\n",
1172                lsm, lli->lli_smd, inode->i_ino, inode->i_generation, inode);
1173         lli->lli_smd = lsm;
1174         lli->lli_maxbytes = lsm->lsm_maxbytes;
1175         if (lli->lli_maxbytes > PAGE_CACHE_MAXBYTES)
1176                 lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
1177 }
1178
1179 void ll_update_inode(struct inode *inode, struct lustre_md *md)
1180 {
1181         struct ll_inode_info *lli = ll_i2info(inode);
1182         struct mds_body *body = md->body;
1183         struct lov_stripe_md *lsm = md->lsm;
1184
1185         LASSERT ((lsm != NULL) == ((body->valid & OBD_MD_FLEASIZE) != 0));
1186         if (lsm != NULL) {
1187                 if (lli->lli_smd == NULL) {
1188                         if (lsm->lsm_magic != LOV_MAGIC && 
1189                             lsm->lsm_magic != LOV_MAGIC_JOIN) {
1190                                 dump_lsm(D_ERROR, lsm);
1191                                 LBUG();
1192                         }
1193                         CDEBUG(D_INODE, "adding lsm %p to inode %lu/%u(%p)\n",
1194                                lsm, inode->i_ino, inode->i_generation, inode);
1195                         /* ll_inode_size_lock() requires it is only called
1196                          * with lli_smd != NULL or lock_lsm == 0 or we can
1197                          * race between lock/unlock.  bug 9547 */
1198                         lli->lli_smd = lsm;
1199                         lli->lli_maxbytes = lsm->lsm_maxbytes;
1200                         if (lli->lli_maxbytes > PAGE_CACHE_MAXBYTES)
1201                                 lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
1202                 } else {
1203                         if (lli->lli_smd->lsm_magic == lsm->lsm_magic &&
1204                              lli->lli_smd->lsm_stripe_count == 
1205                                         lsm->lsm_stripe_count) {
1206                                 if (lov_stripe_md_cmp(lli->lli_smd, lsm)) {
1207                                         CERROR("lsm mismatch for inode %ld\n",
1208                                                 inode->i_ino);
1209                                         CERROR("lli_smd:\n");
1210                                         dump_lsm(D_ERROR, lli->lli_smd);
1211                                         CERROR("lsm:\n");
1212                                         dump_lsm(D_ERROR, lsm);
1213                                         LBUG();
1214                                 }
1215                         } else 
1216                                 ll_replace_lsm(inode, lsm);
1217                 }
1218                 /* bug 2844 - limit i_blksize for broken user-space apps */
1219                 LASSERTF(lsm->lsm_xfersize != 0, "%lu\n", lsm->lsm_xfersize);
1220                 inode->i_blksize = min(lsm->lsm_xfersize, LL_MAX_BLKSIZE);
1221                 if (lli->lli_smd != lsm)
1222                         obd_free_memmd(ll_i2obdexp(inode), &lsm);
1223         } else {
1224                 inode->i_blksize = max(inode->i_blksize,
1225                                        inode->i_sb->s_blocksize);
1226         }
1227
1228 #ifdef CONFIG_FS_POSIX_ACL
1229         LASSERT(!md->posix_acl || (body->valid & OBD_MD_FLACL));
1230         if (body->valid & OBD_MD_FLACL) {
1231                 spin_lock(&lli->lli_lock);
1232                 if (lli->lli_posix_acl)
1233                         posix_acl_release(lli->lli_posix_acl);
1234                 lli->lli_posix_acl = md->posix_acl;
1235                 spin_unlock(&lli->lli_lock);
1236         }
1237 #endif
1238
1239         if (body->valid & OBD_MD_FLID)
1240                 inode->i_ino = body->ino;
1241         if (body->valid & OBD_MD_FLATIME)
1242                 LTIME_S(inode->i_atime) = body->atime;
1243         if (body->valid & OBD_MD_FLMTIME &&
1244             body->mtime > LTIME_S(inode->i_mtime)) {
1245                 CDEBUG(D_INODE, "setting ino %lu mtime from %lu to "LPU64"\n",
1246                        inode->i_ino, LTIME_S(inode->i_mtime), body->mtime);
1247                 LTIME_S(inode->i_mtime) = body->mtime;
1248         }
1249         if (body->valid & OBD_MD_FLCTIME &&
1250             body->ctime > LTIME_S(inode->i_ctime))
1251                 LTIME_S(inode->i_ctime) = body->ctime;
1252         if (body->valid & OBD_MD_FLMODE)
1253                 inode->i_mode = (inode->i_mode & S_IFMT)|(body->mode & ~S_IFMT);
1254         if (body->valid & OBD_MD_FLTYPE)
1255                 inode->i_mode = (inode->i_mode & ~S_IFMT)|(body->mode & S_IFMT);
1256         if (body->valid & OBD_MD_FLUID)
1257                 inode->i_uid = body->uid;
1258         if (body->valid & OBD_MD_FLGID)
1259                 inode->i_gid = body->gid;
1260         if (body->valid & OBD_MD_FLFLAGS)
1261                 inode->i_flags = body->flags;
1262         if (body->valid & OBD_MD_FLNLINK)
1263                 inode->i_nlink = body->nlink;
1264         if (body->valid & OBD_MD_FLGENER)
1265                 inode->i_generation = body->generation;
1266         if (body->valid & OBD_MD_FLRDEV)
1267 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1268                 inode->i_rdev = body->rdev;
1269 #else
1270                 inode->i_rdev = old_decode_dev(body->rdev);
1271 #endif
1272         if (body->valid & OBD_MD_FLSIZE)
1273                 inode->i_size = body->size;
1274         if (body->valid & OBD_MD_FLBLOCKS)
1275                 inode->i_blocks = body->blocks;
1276
1277         if (body->valid & OBD_MD_FLSIZE)
1278                 set_bit(LLI_F_HAVE_MDS_SIZE_LOCK, &lli->lli_flags);
1279 }
1280
1281 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
1282 static struct backing_dev_info ll_backing_dev_info = {
1283         .ra_pages       = 0,    /* No readahead */
1284 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12))
1285         .capabilities   = 0,    /* Does contribute to dirty memory */
1286 #else
1287         .memory_backed  = 0,    /* Does contribute to dirty memory */
1288 #endif
1289 };
1290 #endif
1291
1292 void ll_read_inode2(struct inode *inode, void *opaque)
1293 {
1294         struct lustre_md *md = opaque;
1295         struct ll_inode_info *lli = ll_i2info(inode);
1296         ENTRY;
1297
1298         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
1299                inode->i_generation, inode);
1300
1301         ll_lli_init(lli);
1302
1303         LASSERT(!lli->lli_smd);
1304
1305         /* Core attributes from the MDS first.  This is a new inode, and
1306          * the VFS doesn't zero times in the core inode so we have to do
1307          * it ourselves.  They will be overwritten by either MDS or OST
1308          * attributes - we just need to make sure they aren't newer. */
1309         LTIME_S(inode->i_mtime) = 0;
1310         LTIME_S(inode->i_atime) = 0;
1311         LTIME_S(inode->i_ctime) = 0;
1312         inode->i_rdev = 0;
1313         ll_update_inode(inode, md);
1314
1315         /* OIDEBUG(inode); */
1316
1317         if (S_ISREG(inode->i_mode)) {
1318                 struct ll_sb_info *sbi = ll_i2sbi(inode);
1319                 inode->i_op = &ll_file_inode_operations;
1320                 inode->i_fop = sbi->ll_fop;
1321                 inode->i_mapping->a_ops = &ll_aops;
1322                 EXIT;
1323         } else if (S_ISDIR(inode->i_mode)) {
1324                 inode->i_op = &ll_dir_inode_operations;
1325                 inode->i_fop = &ll_dir_operations;
1326                 inode->i_mapping->a_ops = &ll_dir_aops;
1327                 EXIT;
1328         } else if (S_ISLNK(inode->i_mode)) {
1329                 inode->i_op = &ll_fast_symlink_inode_operations;
1330                 EXIT;
1331         } else {
1332                 inode->i_op = &ll_special_inode_operations;
1333
1334 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1335                 init_special_inode(inode, inode->i_mode,
1336                                    kdev_t_to_nr(inode->i_rdev));
1337
1338                 /* initializing backing dev info. */
1339                 inode->i_mapping->backing_dev_info = &ll_backing_dev_info;
1340 #else
1341                 init_special_inode(inode, inode->i_mode, inode->i_rdev);
1342 #endif
1343                 lli->ll_save_ifop = inode->i_fop;
1344
1345                 if (S_ISCHR(inode->i_mode))
1346                         inode->i_fop = &ll_special_chr_inode_fops;
1347                 else if (S_ISBLK(inode->i_mode))
1348                         inode->i_fop = &ll_special_blk_inode_fops;
1349                 else if (S_ISFIFO(inode->i_mode))
1350                         inode->i_fop = &ll_special_fifo_inode_fops;
1351                 else if (S_ISSOCK(inode->i_mode))
1352                         inode->i_fop = &ll_special_sock_inode_fops;
1353                 EXIT;
1354         }
1355 }
1356
1357 int ll_iocontrol(struct inode *inode, struct file *file,
1358                  unsigned int cmd, unsigned long arg)
1359 {
1360         struct ll_sb_info *sbi = ll_i2sbi(inode);
1361         struct ptlrpc_request *req = NULL;
1362         int rc, flags = 0;
1363         ENTRY;
1364
1365         switch(cmd) {
1366         case EXT3_IOC_GETFLAGS: {
1367                 struct ll_fid fid;
1368                 struct mds_body *body;
1369
1370                 ll_inode2fid(&fid, inode);
1371                 rc = mdc_getattr(sbi->ll_mdc_exp, &fid, OBD_MD_FLFLAGS,0,&req);
1372                 if (rc) {
1373                         CERROR("failure %d inode %lu\n", rc, inode->i_ino);
1374                         RETURN(-abs(rc));
1375                 }
1376
1377                 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
1378
1379                 if (body->flags & S_APPEND)
1380                         flags |= EXT3_APPEND_FL;
1381                 if (body->flags & S_IMMUTABLE)
1382                         flags |= EXT3_IMMUTABLE_FL;
1383                 if (body->flags & S_NOATIME)
1384                         flags |= EXT3_NOATIME_FL;
1385
1386                 ptlrpc_req_finished (req);
1387
1388                 RETURN(put_user(flags, (int *)arg));
1389         }
1390         case EXT3_IOC_SETFLAGS: {
1391                 struct mdc_op_data op_data;
1392                 struct iattr attr;
1393                 struct obdo *oa;
1394                 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
1395
1396                 if (get_user(flags, (int *)arg))
1397                         RETURN(-EFAULT);
1398
1399                 oa = obdo_alloc();
1400                 if (!oa)
1401                         RETURN(-ENOMEM);
1402
1403                 ll_prepare_mdc_op_data(&op_data, inode, NULL, NULL, 0, 0);
1404
1405                 memset(&attr, 0x0, sizeof(attr));
1406                 attr.ia_attr_flags = flags;
1407                 attr.ia_valid |= ATTR_ATTR_FLAG;
1408
1409                 rc = mdc_setattr(sbi->ll_mdc_exp, &op_data,
1410                                  &attr, NULL, 0, NULL, 0, &req);
1411                 if (rc) {
1412                         ptlrpc_req_finished(req);
1413                         if (rc != -EPERM && rc != -EACCES)
1414                                 CERROR("mdc_setattr fails: rc = %d\n", rc);
1415                         obdo_free(oa);
1416                         RETURN(rc);
1417                 }
1418                 ptlrpc_req_finished(req);
1419
1420                 oa->o_id = lsm->lsm_object_id;
1421                 oa->o_flags = flags;
1422                 oa->o_valid = OBD_MD_FLID | OBD_MD_FLFLAGS;
1423
1424                 obdo_from_inode(oa, inode, OBD_MD_FLFID | OBD_MD_FLGENER);
1425                 rc = obd_setattr(sbi->ll_osc_exp, oa, lsm, NULL);
1426                 obdo_free(oa);
1427                 if (rc) {
1428                         if (rc != -EPERM && rc != -EACCES)
1429                                 CERROR("mdc_setattr fails: rc = %d\n", rc);
1430                         RETURN(rc);
1431                 }
1432
1433                 if (flags & EXT3_APPEND_FL)
1434                         inode->i_flags |= S_APPEND;
1435                 else
1436                         inode->i_flags &= ~S_APPEND;
1437                 if (flags & EXT3_IMMUTABLE_FL)
1438                         inode->i_flags |= S_IMMUTABLE;
1439                 else
1440                         inode->i_flags &= ~S_IMMUTABLE;
1441                 if (flags & EXT3_NOATIME_FL)
1442                         inode->i_flags |= S_NOATIME;
1443                 else
1444                         inode->i_flags &= ~S_NOATIME;
1445
1446                 RETURN(0);
1447         }
1448         default:
1449                 RETURN(-ENOSYS);
1450         }
1451
1452         RETURN(0);
1453 }
1454
1455 void ll_umount_begin(struct super_block *sb)
1456 {
1457         struct lustre_sb_info *lsi = s2lsi(sb);
1458         struct ll_sb_info *sbi = ll_s2sbi(sb);
1459         struct obd_device *obd;
1460         struct obd_ioctl_data ioc_data = { 0 };
1461         ENTRY;
1462
1463         /* Make the MGC not try to cancel locks */
1464         lsi->lsi_flags |= LSI_UMOUNT_FAILOVER;
1465
1466         CDEBUG(D_VFSTRACE, "VFS Op: superblock %p count %d active %d\n", sb,
1467                sb->s_count, atomic_read(&sb->s_active));
1468
1469         obd = class_exp2obd(sbi->ll_mdc_exp);
1470         if (obd == NULL) {
1471                 CERROR("Invalid MDC connection handle "LPX64"\n",
1472                        sbi->ll_mdc_exp->exp_handle.h_cookie);
1473                 EXIT;
1474                 return;
1475         }
1476         obd->obd_no_recov = 1;
1477         obd_iocontrol(IOC_OSC_SET_ACTIVE, sbi->ll_mdc_exp, sizeof ioc_data,
1478                       &ioc_data, NULL);
1479
1480         obd = class_exp2obd(sbi->ll_osc_exp);
1481         if (obd == NULL) {
1482                 CERROR("Invalid LOV connection handle "LPX64"\n",
1483                        sbi->ll_osc_exp->exp_handle.h_cookie);
1484                 EXIT;
1485                 return;
1486         }
1487         obd->obd_no_recov = 1;
1488         obd_iocontrol(IOC_OSC_SET_ACTIVE, sbi->ll_osc_exp, sizeof ioc_data,
1489                       &ioc_data, NULL);
1490
1491         /* Really, we'd like to wait until there are no requests outstanding,
1492          * and then continue.  For now, we just invalidate the requests,
1493          * schedule, and hope.
1494          */
1495         schedule();
1496
1497         EXIT;
1498 }
1499
1500 int ll_remount_fs(struct super_block *sb, int *flags, char *data)
1501 {
1502         struct ll_sb_info *sbi = ll_s2sbi(sb);
1503         int err;
1504         __u32 read_only;
1505  
1506         if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY)) {
1507                 read_only = *flags & MS_RDONLY;
1508                 err = obd_set_info(sbi->ll_mdc_exp, strlen("read-only"),
1509                                    "read-only", sizeof(read_only), &read_only);
1510                 if (err) {
1511                         CERROR("Failed to change the read-only flag during "
1512                                "remount: %d\n", err);
1513                         return err;
1514                 }
1515  
1516                 if (read_only)
1517                         sb->s_flags |= MS_RDONLY;
1518                 else
1519                         sb->s_flags &= ~MS_RDONLY;
1520         }
1521         return 0;
1522 }
1523
1524 int ll_prep_inode(struct obd_export *exp, struct inode **inode,
1525                   struct ptlrpc_request *req, int offset,struct super_block *sb)
1526 {
1527         struct lustre_md md;
1528         struct ll_sb_info *sbi = NULL;
1529         int rc = 0;
1530         ENTRY;
1531
1532         LASSERT(*inode || sb);
1533         sbi = sb ? ll_s2sbi(sb) : ll_i2sbi(*inode);
1534         prune_deathrow(sbi, 1);
1535
1536         rc = mdc_req2lustre_md(req, offset, exp, &md);
1537         if (rc)
1538                 RETURN(rc);
1539
1540         if (*inode) {
1541                 ll_update_inode(*inode, &md);
1542         } else {
1543                 LASSERT(sb);
1544                 *inode = ll_iget(sb, md.body->ino, &md);
1545                 if (*inode == NULL || is_bad_inode(*inode)) {
1546                         mdc_free_lustre_md(exp, &md);
1547                         rc = -ENOMEM;
1548                         CERROR("new_inode -fatal: rc %d\n", rc);
1549                         GOTO(out, rc);
1550                 }
1551         }
1552
1553         rc = obd_checkmd(exp, ll_i2mdcexp(*inode),
1554                          ll_i2info(*inode)->lli_smd);
1555 out:
1556         RETURN(rc);
1557 }
1558
1559 char *llap_origins[] = {
1560         [LLAP_ORIGIN_UNKNOWN] = "--",
1561         [LLAP_ORIGIN_READPAGE] = "rp",
1562         [LLAP_ORIGIN_READAHEAD] = "ra",
1563         [LLAP_ORIGIN_COMMIT_WRITE] = "cw",
1564         [LLAP_ORIGIN_WRITEPAGE] = "wp",
1565 };
1566
1567 struct ll_async_page *llite_pglist_next_llap(struct ll_sb_info *sbi,
1568                                              struct list_head *list)
1569 {
1570         struct ll_async_page *llap;
1571         struct list_head *pos;
1572
1573         list_for_each(pos, list) {
1574                 if (pos == &sbi->ll_pglist)
1575                         return NULL;
1576                 llap = list_entry(pos, struct ll_async_page, llap_pglist_item);
1577                 if (llap->llap_page == NULL)
1578                         continue;
1579                 return llap;
1580         }
1581         LBUG();
1582         return NULL;
1583 }
1584
1585 EXPORT_SYMBOL(ll_fill_super);
1586 EXPORT_SYMBOL(ll_put_super);
1587 EXPORT_SYMBOL(ll_remount_fs);
1588 EXPORT_SYMBOL(ll_umount_begin);
1589