Whamcloud - gitweb
Hold dentry reference over log close so it is valid for unlink.
[fs/lustre-release.git] / lustre / obdclass / llog_lvfs.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2001-2003 Cluster File Systems, Inc.
5  *   Author: Andreas Dilger <adilger@clusterfs.com>
6  *
7  *   This file is part of Lustre, http://www.lustre.org.
8  *
9  *   Lustre is free software; you can redistribute it and/or
10  *   modify it under the terms of version 2 of the GNU General Public
11  *   License as published by the Free Software Foundation.
12  *
13  *   Lustre is distributed in the hope that it will be useful,
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *   GNU General Public License for more details.
17  *
18  *   You should have received a copy of the GNU General Public License
19  *   along with Lustre; if not, write to the Free Software
20  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  * OST<->MDS recovery logging infrastructure.
23  *
24  * Invariants in implementation:
25  * - we do not share logs among different OST<->MDS connections, so that
26  *   if an OST or MDS fails it need only look at log(s) relevant to itself
27  */
28
29 #define DEBUG_SUBSYSTEM S_LOG
30
31 #ifndef EXPORT_SYMTAB
32 #define EXPORT_SYMTAB
33 #endif
34
35 #ifdef __KERNEL__
36 #include <linux/fs.h>
37 #else
38 #include <liblustre.h>
39 #endif
40
41 #include <linux/obd.h>
42 #include <linux/obd_class.h>
43 #include <linux/lustre_log.h>
44 #include <linux/obd_ost.h>
45 #include <portals/list.h>
46 #include <linux/lvfs.h>
47 #include <linux/lustre_fsfilt.h>
48 #include "llog_internal.h"
49
50 #ifdef __KERNEL__
51
52 static int llog_lvfs_pad(struct obd_device *obd, struct l_file *file,
53                                 int len, int index)
54 {
55         struct llog_rec_hdr rec = { 0 };
56         struct llog_rec_tail tail;
57         int rc;
58         ENTRY;
59
60         LASSERT(len >= LLOG_MIN_REC_SIZE && (len & 0x7) == 0);
61
62         tail.lrt_len = rec.lrh_len = len;
63         tail.lrt_index = rec.lrh_index = index;
64         rec.lrh_type = 0;
65
66         rc = fsfilt_write_record(obd, file, &rec, sizeof(rec), &file->f_pos, 0);
67         if (rc) {
68                 CERROR("error writing padding record: rc %d\n", rc);
69                 goto out;
70         }
71
72         file->f_pos += len - sizeof(rec) - sizeof(tail);
73         rc = fsfilt_write_record(obd, file, &tail, sizeof(tail),&file->f_pos,0);
74         if (rc) {
75                 CERROR("error writing padding record: rc %d\n", rc);
76                 goto out;
77         }
78
79  out:
80         RETURN(rc);
81 }
82
83 static int llog_lvfs_write_blob(struct obd_device *obd, struct l_file *file,
84                                 struct llog_rec_hdr *rec, void *buf, loff_t off)
85 {
86         int rc;
87         struct llog_rec_tail end;
88         loff_t saved_off = file->f_pos;
89         int buflen = rec->lrh_len;
90
91         ENTRY;
92         file->f_pos = off;
93
94         if (!buf) {
95                 rc = fsfilt_write_record(obd, file, rec, buflen,&file->f_pos,0);
96                 if (rc) {
97                         CERROR("error writing log record: rc %d\n", rc);
98                         goto out;
99                 }
100                 GOTO(out, rc = 0);
101         }
102
103         /* the buf case */
104         rec->lrh_len = sizeof(*rec) + buflen + sizeof(end);
105         rc = fsfilt_write_record(obd, file, rec, sizeof(*rec), &file->f_pos, 0);
106         if (rc) {
107                 CERROR("error writing log hdr: rc %d\n", rc);
108                 goto out;
109         }
110
111         rc = fsfilt_write_record(obd, file, buf, buflen, &file->f_pos, 0);
112         if (rc) {
113                 CERROR("error writing log buffer: rc %d\n", rc);
114                 goto out;
115         }
116
117         end.lrt_len = rec->lrh_len;
118         end.lrt_index = rec->lrh_index;
119         rc = fsfilt_write_record(obd, file, &end, sizeof(end), &file->f_pos, 0);
120         if (rc) {
121                 CERROR("error writing log tail: rc %d\n", rc);
122                 goto out;
123         }
124
125         rc = 0;
126  out:
127         if (saved_off > file->f_pos)
128                 file->f_pos = saved_off;
129         LASSERT(rc <= 0);
130         RETURN(rc);
131 }
132
133 static int llog_lvfs_read_blob(struct obd_device *obd, struct l_file *file,
134                                 void *buf, int size, loff_t off)
135 {
136         loff_t offset = off;
137         int rc;
138         ENTRY;
139
140         rc = fsfilt_read_record(obd, file, buf, size, &offset);
141         if (rc) {
142                 CERROR("error reading log record: rc %d\n", rc);
143                 RETURN(rc);
144         }
145         RETURN(0);
146 }
147
148 static int llog_lvfs_read_header(struct llog_handle *handle)
149 {
150         struct obd_device *obd;
151         int rc;
152         ENTRY;
153
154         LASSERT(sizeof(*handle->lgh_hdr) == LLOG_CHUNK_SIZE);
155
156         obd = handle->lgh_ctxt->loc_exp->exp_obd;
157
158         if (handle->lgh_file->f_dentry->d_inode->i_size == 0) {
159                 CDEBUG(D_HA, "not reading header from 0-byte log\n");
160                 RETURN(LLOG_EEMPTY);
161         }
162
163         rc = llog_lvfs_read_blob(obd, handle->lgh_file, handle->lgh_hdr,
164                                  LLOG_CHUNK_SIZE, 0);
165         if (rc) {
166                 CERROR("error reading log header\n");
167         } else {
168                 struct llog_rec_hdr *llh_hdr = &handle->lgh_hdr->llh_hdr;
169                 /*
170                  * These need to be fixed for bug 1987
171                  */
172                 if (llh_hdr->lrh_type != LLOG_HDR_MAGIC) {
173                         CERROR("bad log header magic: %#x (expecting %#x)\n",
174                                llh_hdr->lrh_type, LLOG_HDR_MAGIC);
175                         rc = -EIO;
176                 } else if (llh_hdr->lrh_len != LLOG_CHUNK_SIZE) {
177                         CERROR("incorrectly sized log header: %#x "
178                                "(expecting %#x)\n",
179                                llh_hdr->lrh_len, LLOG_CHUNK_SIZE);
180                         CERROR("you may need to re-run lconf --write_conf.\n");
181                         rc = -EIO;
182                 }
183         }
184         
185
186         handle->lgh_last_idx = handle->lgh_hdr->llh_tail.lrt_index;
187         handle->lgh_file->f_pos = handle->lgh_file->f_dentry->d_inode->i_size;
188
189         RETURN(rc);
190 }
191
192 /* returns negative in on error; 0 if success && reccookie == 0; 1 otherwise */
193 /* appends if idx == -1, otherwise overwrites record idx. */
194 static int llog_lvfs_write_rec(struct llog_handle *loghandle,
195                                struct llog_rec_hdr *rec,
196                                struct llog_cookie *reccookie, int cookiecount,
197                                void *buf, int idx)
198 {
199         struct llog_log_hdr *llh;
200         int reclen = rec->lrh_len, index, rc;
201         struct llog_rec_tail *lrt;
202         struct obd_device *obd;
203         struct file *file;
204         size_t left;
205         ENTRY;
206
207         llh = loghandle->lgh_hdr;
208         file = loghandle->lgh_file;
209         obd = loghandle->lgh_ctxt->loc_exp->exp_obd;
210
211         /* record length should not bigger than LLOG_CHUNK_SIZE */
212         if (buf)
213                 rc = (reclen > LLOG_CHUNK_SIZE - sizeof(struct llog_rec_hdr) -
214                       sizeof(struct llog_rec_tail)) ? -E2BIG : 0;
215         else
216                 rc = (reclen > LLOG_CHUNK_SIZE) ? -E2BIG : 0;
217         if (rc)
218                 RETURN(rc);
219
220         if (idx != -1) {
221                 loff_t saved_offset;
222
223                 /* no header: only allowed to insert record 1 */
224                 if (idx != 1 && !file->f_dentry->d_inode->i_size) {
225                         CERROR("idx != -1 in empty log\n");
226                         LBUG();
227                 }
228
229                 if (idx && llh->llh_size && llh->llh_size != reclen)
230                         RETURN(-EINVAL);
231
232                 rc = llog_lvfs_write_blob(obd, file, &llh->llh_hdr, NULL, 0);
233                 /* we are done if we only write the header or on error */
234                 if (rc || idx == 0)
235                         RETURN(rc);
236
237                 saved_offset = sizeof(*llh) + (idx-1)*rec->lrh_len;
238                 rc = llog_lvfs_write_blob(obd, file, rec, buf, saved_offset);
239                 if (rc == 0 && reccookie) {
240                         reccookie->lgc_lgl = loghandle->lgh_id;
241                         reccookie->lgc_index = idx;
242                         rc = 1;
243                 }
244                 RETURN(rc);
245         }
246
247         /* Make sure that records don't cross a chunk boundary, so we can
248          * process them page-at-a-time if needed.  If it will cross a chunk
249          * boundary, write in a fake (but referenced) entry to pad the chunk.
250          *
251          * We know that llog_current_log() will return a loghandle that is
252          * big enough to hold reclen, so all we care about is padding here.
253          */
254         left = LLOG_CHUNK_SIZE - (file->f_pos & (LLOG_CHUNK_SIZE - 1));
255         if (buf)
256                 reclen = sizeof(*rec) + rec->lrh_len + 
257                         sizeof(struct llog_rec_tail);
258
259         /* NOTE: padding is a record, but no bit is set */
260         if (left != 0 && left != reclen &&
261             left < (reclen + LLOG_MIN_REC_SIZE)) {
262                 int bitmap_size = sizeof(llh->llh_bitmap) * 8;
263                 loghandle->lgh_last_idx++;
264                 rc = llog_lvfs_pad(obd, file, left, loghandle->lgh_last_idx);
265                 if (rc)
266                         RETURN(rc);
267                 /* if it's the last idx in log file, then return -ENOSPC */
268                 if (loghandle->lgh_last_idx == bitmap_size - 1)
269                         RETURN(-ENOSPC);
270         }
271
272         loghandle->lgh_last_idx++;
273         index = loghandle->lgh_last_idx;
274         rec->lrh_index = index;
275         if (buf == NULL) {
276                 lrt = (struct llog_rec_tail *)
277                         ((char *)rec + rec->lrh_len - sizeof(*lrt));
278                 lrt->lrt_len = rec->lrh_len;
279                 lrt->lrt_index = rec->lrh_index;
280         }
281         if (ext2_set_bit(index, llh->llh_bitmap)) {
282                 CERROR("argh, index %u already set in log bitmap?\n", index);
283                 LBUG(); /* should never happen */
284         }
285         llh->llh_count++;
286         llh->llh_tail.lrt_index = index;
287
288         rc = llog_lvfs_write_blob(obd, file, &llh->llh_hdr, NULL, 0);
289         if (rc)
290                 RETURN(rc);
291
292         rc = llog_lvfs_write_blob(obd, file, rec, buf, file->f_pos);
293         if (rc)
294                 RETURN(rc);
295
296         CDEBUG(D_HA, "added record "LPX64": idx: %u, %u bytes\n",
297                loghandle->lgh_id.lgl_oid, index, rec->lrh_len);
298         if (rc == 0 && reccookie) {
299                 reccookie->lgc_lgl = loghandle->lgh_id;
300                 reccookie->lgc_index = index;
301                 if (rec->lrh_type == MDS_UNLINK_REC)
302                         reccookie->lgc_subsys = LLOG_UNLINK_ORIG_CTXT;
303                 else if (rec->lrh_type == OST_SZ_REC)
304                         reccookie->lgc_subsys = LLOG_SIZE_ORIG_CTXT;
305                 else if (rec->lrh_type == OST_RAID1_REC)
306                         reccookie->lgc_subsys = LLOG_RD1_ORIG_CTXT;
307                 else
308                         reccookie->lgc_subsys = -1;
309                 rc = 1;
310         }
311         if (rc == 0 && rec->lrh_type == LLOG_GEN_REC)
312                 rc = 1;
313
314         RETURN(rc);
315 }
316
317 /* We can skip reading at least as many log blocks as the number of
318 * minimum sized log records we are skipping.  If it turns out
319 * that we are not far enough along the log (because the
320 * actual records are larger than minimum size) we just skip
321 * some more records. */
322
323 static void llog_skip_over(__u64 *off, int curr, int goal)
324 {
325         if (goal <= curr)
326                 return;
327         *off = (*off + (goal-curr-1) * LLOG_MIN_REC_SIZE) &
328                 ~(LLOG_CHUNK_SIZE - 1);
329 }
330
331
332 /* sets:
333  *  - cur_offset to the furthest point read in the log file
334  *  - cur_idx to the log index preceeding cur_offset
335  * returns -EIO/-EINVAL on error
336  */
337 static int llog_lvfs_next_block(struct llog_handle *loghandle, int *cur_idx,
338                                 int next_idx, __u64 *cur_offset, void *buf,
339                                 int len)
340 {
341         int rc;
342         ENTRY;
343
344         if (len == 0 || len & (LLOG_CHUNK_SIZE - 1))
345                 RETURN(-EINVAL);
346
347         CDEBUG(D_OTHER, "looking for log index %u (cur idx %u off "LPU64")\n",
348                next_idx, *cur_idx, *cur_offset);
349
350         while (*cur_offset < loghandle->lgh_file->f_dentry->d_inode->i_size) {
351                 struct llog_rec_hdr *rec;
352                 struct llog_rec_tail *tail;
353                 loff_t ppos;
354
355                 llog_skip_over(cur_offset, *cur_idx, next_idx);
356
357                 ppos = *cur_offset;
358                 rc = fsfilt_read_record(loghandle->lgh_ctxt->loc_exp->exp_obd,
359                                         loghandle->lgh_file, buf, len,
360                                         &ppos);
361
362                 if (rc) {
363                         CERROR("Cant read llog block at log id "LPU64
364                                "/%u offset "LPU64"\n",
365                                loghandle->lgh_id.lgl_oid,
366                                loghandle->lgh_id.lgl_ogen,
367                                *cur_offset);
368                         RETURN(rc);
369                 }
370
371                 /* put number of bytes read into rc to make code simpler */
372                 rc = ppos - *cur_offset;
373                 *cur_offset = ppos;
374
375                 if (rc == 0) /* end of file, nothing to do */
376                         RETURN(0);
377
378                 if (rc < sizeof(*tail)) {
379                         CERROR("Invalid llog block at log id "LPU64"/%u offset "
380                                LPU64"\n", loghandle->lgh_id.lgl_oid,
381                                loghandle->lgh_id.lgl_ogen, *cur_offset);
382                         RETURN(-EINVAL);
383                 }
384
385                 tail = buf + rc - sizeof(struct llog_rec_tail);
386                 *cur_idx = tail->lrt_index;
387
388                 /* this shouldn't happen */
389                 if (tail->lrt_index == 0) {
390                         CERROR("Invalid llog tail at log id "LPU64"/%u offset "
391                                LPU64"\n", loghandle->lgh_id.lgl_oid,
392                                loghandle->lgh_id.lgl_ogen, *cur_offset);
393                         RETURN(-EINVAL);
394                 }
395                 if (tail->lrt_index < next_idx)
396                         continue;
397
398                 /* sanity check that the start of the new buffer is no farther
399                  * than the record that we wanted.  This shouldn't happen. */
400                 rec = buf;
401                 if (rec->lrh_index > next_idx) {
402                         CERROR("missed desired record? %u > %u\n",
403                                rec->lrh_index, next_idx);
404                         RETURN(-ENOENT);
405                 }
406                 RETURN(0);
407         }
408         RETURN(-EIO);
409 }
410
411 static struct file *llog_filp_open(char *name, int flags, int mode)
412 {
413         char *logname;
414         struct file *filp;
415         int len;
416
417         OBD_ALLOC(logname, PATH_MAX);
418         if (logname == NULL)
419                 return ERR_PTR(-ENOMEM);
420
421         len = snprintf(logname, PATH_MAX, "LOGS/%s", name);
422         if (len >= PATH_MAX - 1) {
423                 filp = ERR_PTR(-ENAMETOOLONG);
424         } else {
425                 filp = l_filp_open(logname, flags, mode);
426                 if (IS_ERR(filp))
427                         CERROR("logfile creation %s: %ld\n", logname,
428                                PTR_ERR(filp));
429         }
430
431         OBD_FREE(logname, PATH_MAX);
432         return filp;
433 }
434
435 /* This is a callback from the llog_* functions.
436  * Assumes caller has already pushed us into the kernel context. */
437 static int llog_lvfs_create(struct llog_ctxt *ctxt, struct llog_handle **res,
438                             struct llog_logid *logid, char *name)
439 {
440         struct llog_handle *handle;
441         struct obd_device *obd;
442         struct l_dentry *dchild = NULL;
443         struct obdo *oa = NULL;
444         int rc = 0, cleanup_phase = 1;
445         int open_flags = O_RDWR | O_CREAT | O_LARGEFILE;
446         ENTRY;
447
448         handle = llog_alloc_handle();
449         if (handle == NULL)
450                 RETURN(-ENOMEM);
451         *res = handle;
452
453         LASSERT(ctxt);
454         LASSERT(ctxt->loc_exp);
455         obd = ctxt->loc_exp->exp_obd;
456
457         if (logid != NULL) {
458                 dchild = obd_lvfs_fid2dentry(ctxt->loc_exp, logid->lgl_oid,
459                                              logid->lgl_ogen, logid->lgl_ogr);
460
461                 if (IS_ERR(dchild)) {
462                         rc = PTR_ERR(dchild);
463                         CERROR("error looking up logfile "LPX64":0x%x: rc %d\n",
464                                logid->lgl_oid, logid->lgl_ogen, rc);
465                         GOTO(cleanup, rc);
466                 }
467
468                 cleanup_phase = 2;
469                 if (dchild->d_inode == NULL) {
470                         rc = -ENOENT;
471                         CERROR("nonexistent log file "LPX64":"LPX64": rc %d\n",
472                                logid->lgl_oid, logid->lgl_ogr, rc);
473                         GOTO(cleanup, rc);
474                 }
475
476                 handle->lgh_file = l_dentry_open(&obd->obd_ctxt, dchild,
477                                                     O_RDWR | O_LARGEFILE);
478                 if (IS_ERR(handle->lgh_file)) {
479                         rc = PTR_ERR(handle->lgh_file);
480                         CERROR("error opening logfile "LPX64"0x%x: rc %d\n",
481                                logid->lgl_oid, logid->lgl_ogen, rc);
482                         GOTO(cleanup, rc);
483                 }
484
485                 /* assign the value of lgh_id for handle directly */
486                 handle->lgh_id = *logid;
487
488         } else if (name) {
489                 handle->lgh_file = llog_filp_open(name, open_flags, 0644);
490                 if (IS_ERR(handle->lgh_file))
491                         GOTO(cleanup, rc = PTR_ERR(handle->lgh_file));
492
493                 handle->lgh_id.lgl_ogr = 1;
494                 handle->lgh_id.lgl_oid =
495                         handle->lgh_file->f_dentry->d_inode->i_ino;
496                 handle->lgh_id.lgl_ogen =
497                         handle->lgh_file->f_dentry->d_inode->i_generation;
498         } else {
499                 oa = obdo_alloc();
500                 if (oa == NULL)
501                         GOTO(cleanup, rc = -ENOMEM);
502                 /* XXX get some filter group constants */
503                 oa->o_gr = 1;
504                 oa->o_valid = OBD_MD_FLGENER | OBD_MD_FLGROUP;
505                 rc = obd_create(ctxt->loc_exp, oa, NULL, NULL);
506                 if (rc)
507                         GOTO(cleanup, rc);
508
509                 dchild = obd_lvfs_fid2dentry(ctxt->loc_exp, oa->o_id,
510                                              oa->o_generation, oa->o_gr);
511
512                 if (IS_ERR(dchild))
513                         GOTO(cleanup, rc = PTR_ERR(dchild));
514                 cleanup_phase = 2;
515                 handle->lgh_file = l_dentry_open(&obd->obd_ctxt, dchild,
516                                                  open_flags);
517                 if (IS_ERR(handle->lgh_file))
518                         GOTO(cleanup, rc = PTR_ERR(handle->lgh_file));
519
520                 handle->lgh_id.lgl_ogr = oa->o_gr;
521                 handle->lgh_id.lgl_oid = oa->o_id;
522                 handle->lgh_id.lgl_ogen = oa->o_generation;
523         }
524
525         handle->lgh_ctxt = ctxt;
526  finish:
527         if (oa)
528                 obdo_free(oa);
529         RETURN(rc);
530 cleanup:
531         switch (cleanup_phase) {
532         case 2:
533                 l_dput(dchild);
534         case 1:
535                 llog_free_handle(handle);
536         }
537         goto finish;
538 }
539
540 static int llog_lvfs_close(struct llog_handle *handle)
541 {
542         int rc;
543         ENTRY;
544
545         rc = filp_close(handle->lgh_file, 0);
546         if (rc)
547                 CERROR("error closing log: rc %d\n", rc);
548         RETURN(rc);
549 }
550
551 static int llog_lvfs_destroy(struct llog_handle *handle)
552 {
553         struct dentry *fdentry;
554         struct obdo *oa;
555         int rc;
556         ENTRY;
557
558         fdentry = handle->lgh_file->f_dentry;
559         if (!strcmp(fdentry->d_parent->d_name.name, "LOGS")) {
560                 struct obd_device *obd = handle->lgh_ctxt->loc_exp->exp_obd;
561                 struct inode *inode = fdentry->d_parent->d_inode;
562                 struct obd_run_ctxt saved;
563
564                 push_ctxt(&saved, &obd->obd_ctxt, NULL);
565                 dget(fdentry);
566                 rc = llog_lvfs_close(handle);
567                 if (rc)
568                         RETURN(rc);
569
570                 down(&inode->i_sem);
571                 rc = vfs_unlink(inode, fdentry);
572                 up(&inode->i_sem);
573                 dput(fdentry);
574                 pop_ctxt(&saved, &obd->obd_ctxt, NULL);
575                 RETURN(rc);
576         }
577
578         oa = obdo_alloc();
579         if (oa == NULL)
580                 RETURN(-ENOMEM);
581
582         oa->o_id = handle->lgh_id.lgl_oid;
583         oa->o_gr = handle->lgh_id.lgl_ogr;
584         oa->o_generation = handle->lgh_id.lgl_ogen;
585         oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP | OBD_MD_FLGENER;
586
587         rc = llog_lvfs_close(handle);
588         if (rc)
589                 GOTO(out, rc);
590
591         rc = obd_destroy(handle->lgh_ctxt->loc_exp, oa, NULL, NULL);
592  out:
593         obdo_free(oa);
594         RETURN(rc);
595 }
596
597 /* reads the catalog list */
598 int llog_get_cat_list(struct obd_device *obd, struct obd_device *disk_obd,
599                       char *name, int count, struct llog_catid *idarray)
600 {
601         struct obd_run_ctxt saved;
602         struct l_file *file;
603         int rc;
604         int size = sizeof(*idarray) * count;
605         loff_t off = 0;
606
607         LASSERT(count);
608
609         push_ctxt(&saved, &obd->obd_ctxt, NULL);
610         file = filp_open(name, O_RDWR | O_CREAT | O_LARGEFILE, 0700);
611         if (!file || IS_ERR(file)) {
612                 rc = PTR_ERR(file);
613                 CERROR("OBD filter: cannot open/create %s: rc = %d\n",
614                        name, rc);
615                 GOTO(out, rc);
616         }
617
618         if (!S_ISREG(file->f_dentry->d_inode->i_mode)) {
619                 CERROR("%s is not a regular file!: mode = %o\n", name,
620                        file->f_dentry->d_inode->i_mode);
621                 GOTO(out, rc = -ENOENT);
622         }
623
624         rc = fsfilt_read_record(disk_obd, file, idarray, size, &off);
625         if (rc) {
626                 CDEBUG(D_INODE,"OBD filter: error reading %s: rc %d\n",
627                        name, rc);
628                 GOTO(out, rc);
629         }
630
631  out:
632         pop_ctxt(&saved, &obd->obd_ctxt, NULL);
633         if (file && !IS_ERR(file))
634                 rc = filp_close(file, 0);
635         RETURN(rc);
636 }
637 EXPORT_SYMBOL(llog_get_cat_list);
638
639 /* writes the cat list */
640 int llog_put_cat_list(struct obd_device *obd, struct obd_device *disk_obd,
641                       char *name, int count, struct llog_catid *idarray)
642 {
643         struct obd_run_ctxt saved;
644         struct l_file *file;
645         int rc;
646         int size = sizeof(*idarray) * count;
647         loff_t off = 0;
648
649         LASSERT(count);
650
651         push_ctxt(&saved, &obd->obd_ctxt, NULL);
652         file = filp_open(name, O_RDWR | O_CREAT | O_LARGEFILE, 0700);
653         if (!file || IS_ERR(file)) {
654                 rc = PTR_ERR(file);
655                 CERROR("OBD filter: cannot open/create %s: rc = %d\n",
656                        name, rc);
657                 GOTO(out, rc);
658         }
659
660         if (!S_ISREG(file->f_dentry->d_inode->i_mode)) {
661                 CERROR("%s is not a regular file!: mode = %o\n", name,
662                        file->f_dentry->d_inode->i_mode);
663                 GOTO(out, rc = -ENOENT);
664         }
665
666         rc = fsfilt_write_record(disk_obd, file, idarray, size, &off, 1);
667         if (rc) {
668                 CDEBUG(D_INODE,"OBD filter: error reading %s: rc %d\n",
669                        name, rc);
670                 GOTO(out, rc);
671         }
672
673  out:
674         pop_ctxt(&saved, &obd->obd_ctxt, NULL);
675         if (file && !IS_ERR(file))
676                 rc = filp_close(file, 0);
677         RETURN(rc);
678 }
679
680 struct llog_operations llog_lvfs_ops = {
681         lop_write_rec:   llog_lvfs_write_rec,
682         lop_next_block:  llog_lvfs_next_block,
683         lop_read_header: llog_lvfs_read_header,
684         lop_create:      llog_lvfs_create,
685         lop_destroy:     llog_lvfs_destroy,
686         lop_close:       llog_lvfs_close,
687         //        lop_cancel: llog_lvfs_cancel,
688 };
689
690 EXPORT_SYMBOL(llog_lvfs_ops);
691
692 #else /* !__KERNEL__ */
693
694 static int llog_lvfs_read_header(struct llog_handle *handle)
695 {
696         LBUG();
697         return 0;
698 }
699
700 static int llog_lvfs_write_rec(struct llog_handle *loghandle,
701                                struct llog_rec_hdr *rec,
702                                struct llog_cookie *reccookie, int cookiecount,
703                                void *buf, int idx)
704 {
705         LBUG();
706         return 0;
707 }
708
709 static int llog_lvfs_next_block(struct llog_handle *loghandle, int *cur_idx,
710                                 int next_idx, __u64 *cur_offset, void *buf,
711                                 int len)
712 {
713         LBUG();
714         return 0;
715 }
716
717 static int llog_lvfs_create(struct llog_ctxt *ctxt, struct llog_handle **res,
718                             struct llog_logid *logid, char *name)
719 {
720         LBUG();
721         return 0;
722 }
723
724 static int llog_lvfs_close(struct llog_handle *handle)
725 {
726         LBUG();
727         return 0;
728 }
729
730 static int llog_lvfs_destroy(struct llog_handle *handle)
731 {
732         LBUG();
733         return 0;
734 }
735
736 int llog_get_cat_list(struct obd_device *obd, struct obd_device *disk_obd,
737                       char *name, int count, struct llog_logid *idarray)
738 {
739         LBUG();
740         return 0;
741 }
742
743 int llog_put_cat_list(struct obd_device *obd, struct obd_device *disk_obd,
744                       char *name, int count, struct llog_logid *idarray)
745 {
746         LBUG();
747         return 0;
748 }
749
750 struct llog_operations llog_lvfs_ops = {
751         lop_write_rec:   llog_lvfs_write_rec,
752         lop_next_block:  llog_lvfs_next_block,
753         lop_read_header: llog_lvfs_read_header,
754         lop_create:      llog_lvfs_create,
755         lop_destroy:     llog_lvfs_destroy,
756         lop_close:       llog_lvfs_close,
757 //        lop_cancel:      llog_lvfs_cancel,
758 };
759 #endif