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