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