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