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