4 * Copyright (C) 2005-2009
5 * Neil Brown <neilb@suse.de>
6 * Released under the GPL, version 2
12 #include <linux/slab.h>
16 static unsigned char file_type[1 + (S_IFMT >> S_SHIFT)] = {
17 [S_IFREG >> S_SHIFT] = DT_REG,
18 [S_IFDIR >> S_SHIFT] = DT_DIR,
19 [S_IFCHR >> S_SHIFT] = DT_CHR,
20 [S_IFBLK >> S_SHIFT] = DT_BLK,
21 [S_IFIFO >> S_SHIFT] = DT_FIFO,
22 [S_IFSOCK >> S_SHIFT] = DT_SOCK,
23 [S_IFLNK >> S_SHIFT] = DT_LNK,
26 static inline int mode_to_dt(int mode)
28 return file_type[(mode & S_IFMT) >> S_SHIFT];
31 /*****************************************************************************
33 * Most of the work is done in dir_lookup_blk which returns the block
34 * that should hold the entry.
35 * dir_lookup takes that result and finds an inode number if possible.
37 static struct datablock *
38 dir_lookup_blk(struct inode *dir, const char *name, int nlen,
39 u8 *pp, int forcreate, u32 *hashp, REFARG)
41 struct lafs_inode *lai = LAFSI(dir);
42 u32 seed = lai->md.file.seed;
43 struct datablock *b, *cb = NULL;
47 int bits = dir->i_blkbits-8;
51 hash = lafs_hash_name(seed, nlen, name);
52 dprintk("name= %s hash=%lu\n", name, (unsigned long)bn);
57 dprintk("bn=%lu\n", (unsigned long)bn);
58 if (lafs_find_next(dir, &bn) == 0)
60 dprintk("now=%lu\n", (unsigned long)bn);
63 b = lafs_get_block(dir, bn, NULL, GFP_KERNEL, REF);
66 err = lafs_read_block(b);
71 void lafs_dir_print(char *buf, int psz);
72 lafs_dir_print(buf, bits);
74 for ( ; hash < (bn ? bn : MaxDirHash) ; hash++) {
77 int rv = lafs_dir_find(buf, bits, seed, hash, &piece);
90 return ERR_PTR(-ENOENT);
92 lafs_dir_extract(buf, bits, &de, piece, NULL);
94 if (forcreate && !cb) {
102 if (strncmp(de.name, name, de.nlen) != 0)
104 unmap_dblock(b, buf);
109 return ERR_PTR(-EEXIST);
116 unmap_dblock(b, buf);
126 dir_lookup(struct inode *dir, const char *name, int nlen,
138 int bits = dir->i_blkbits-8;
141 b = dir_lookup_blk(dir, name, nlen, &piece, 0, NULL, MKREF(dir_lookup));
145 *inum = lafs_dir_extract(buf, bits, &de, piece, NULL)->target;
146 unmap_dblock(b, buf);
147 putdref(b, MKREF(dir_lookup));
151 /*****************************************************************************
153 * Directory modification routines.
154 * We have sets of prepare, pin, commit, abort.
155 * 'prepare' happens outside of a phase-lock and can allocate
156 * and reserve blocks, and is free to fail. Everything it does
157 * should be revertable - or atomically committed.
158 * 'pin' happens inside a phase-lock and should pin any blocks
159 * that might need to be dirtied. It only fails if the pin fails.
160 * 'commit' is called inside the phase-lock if prepare and pin succeed.
161 * is must clean up anything that was set up by 'prepare'.
162 * 'abort' is called if anything fails. It too should clean up anything
163 * that prepare sets up.
164 * If 'prepare' is called, then either 'commit' or 'abort' will be called
165 * so failure in prepare can leave cleanup to 'abort'.
167 * The whole processes uses a dirop_handle to store various aspects
168 * of state that might need to be unwound or committed.
169 * A compound operation such as rename may included several
170 * simple operations such as delete + create. In that case there
171 * will be a separate dirop_handle for each simple operation.
172 * There will only be one create
174 struct dirop_handle {
175 struct datablock *dirent_block;
176 struct datablock *new;
183 /*............................................................................
184 * Creating an entry in a directory.
185 * This is split into pre_create and commit_create
186 * We already know that the name doesn't exist so a lookup will fail,
187 * but will find the right place in the tree.
188 * pre_create allocates blocks as needed and stores info in the dirop_handle.
189 * commit_create finalises the create and cannot fail.
192 static int dir_create_prepare(struct fs *fs, struct inode *dir,
193 const char *name, int nlen,
195 struct dirop_handle *doh)
198 * We need one or 2 blocks
199 * - a dirent block with space to receive the new entry.
200 * - A free block into which the block index may split
201 * These will be pinned and allocated credits before we commit.
203 * For a single-block which still has room,
204 * we only need the first of those.
207 int blocksize = fs->blocksize;
208 struct datablock *dirblk;
210 int bits = dir->i_blkbits-8;
212 struct lafs_inode *lai = LAFSI(dir);
213 u32 seed = lai->md.file.seed;
214 struct dirheader *dh;
223 dirblk = dir_lookup_blk(dir, name, nlen, &piece, 1, &hash,
227 return PTR_ERR(dirblk);
229 lafs_iolock_written(&dirblk->b);
230 set_bit(B_PinPending, &dirblk->b.flags);
231 /* i_mutex protect us now, so don't need to maintain the lock */
232 lafs_iounlock_block(&dirblk->b);
234 chainoffset = hash - lafs_hash_name(seed, nlen, name);
235 buf = map_dblock(dirblk);
236 rv = lafs_dir_add_ent(buf, bits, name, nlen, 0, DT_TEST,
237 seed, hash, chainoffset);
238 unmap_dblock(dirblk, buf);
243 doh->chainoffset = chainoffset;
246 /* Doesn't fit, try repacking */
248 buf = map_dblock(dirblk);
249 dh = (struct dirheader *)buf;
250 if (dh->freepieces >= space_needed(nlen, chainoffset, bits)) {
252 unmap_dblock(dirblk, buf);
253 tmp = kmalloc(256<<bits, GFP_KERNEL);
255 buf = map_dblock(dirblk);
256 lafs_dir_repack(buf, bits, tmp, seed, 0);
257 if (lafs_dir_add_ent(tmp, bits, name, nlen, 0, DT_TEST,
258 seed, hash, chainoffset)) {
259 memcpy(buf, tmp, blocksize);
260 unmap_dblock(dirblk, buf);
263 doh->chainoffset = chainoffset;
268 unmap_dblock(dirblk, buf);
272 unmap_dblock(dirblk, buf);
274 /* Really doesn't fit, need to split.
275 * We have to perform the split now so that we can choose a new
276 * index and pin that block.
278 n1 = kmalloc(blocksize, GFP_KERNEL);
279 n2 = kmalloc(blocksize, GFP_KERNEL);
281 kfree(n1); kfree(n2);
284 doh->new = lafs_get_block(dir, newhash+1, NULL, GFP_KERNEL,
286 if (doh->new == NULL) {
287 kfree(n1); kfree(n2);
290 buf = map_dblock(dirblk);
291 lafs_dir_split(buf, bits, n1, n2, name, inum, type,
292 &newhash, seed, hash, chainoffset);
293 unmap_dblock(dirblk, buf);
294 buf = map_dblock(doh->new);
295 memcpy(buf, n1, blocksize);
296 unmap_dblock(doh->new, buf);
297 set_bit(B_Valid, &doh->new->b.flags);
298 set_bit(B_PinPending, &doh->new->b.flags);
305 dir_create_commit(struct dirop_handle *doh,
306 struct fs *fs, struct inode *dir,
307 const char *name, int nlen, u32 target, int type)
309 /* We are committed to creating this entry.
310 * Everything has been allocated and pinned.
312 * - add name to doh->dirent_block.
313 * - if doh->index == NULL, done.
314 * - possibly update indexslot to have address of doh->dirent_block.
315 * - if doh->new, split doh->index into doh->new adding doh->previndex
316 * - else add doh->previndex into doh->index
319 int bits = fs->blocksize_bits - 8;
320 int blocksize = fs->blocksize;
321 struct lafs_inode *lai = LAFSI(dir);
322 u32 seed = lai->md.file.seed;
324 buf = map_dblock(doh->dirent_block);
327 /* We did a split, and have the block ready to go */
328 memcpy(buf, doh->temp, blocksize);
331 lafs_dirty_dblock(doh->new);
332 if ((((loff_t)doh->new->b.fileaddr+1) << dir->i_blkbits)
334 i_size_write(dir, (((loff_t)doh->new->b.fileaddr+1)
336 lafs_dirty_inode(dir);
338 clear_bit(B_PinPending, &doh->new->b.flags);
339 putdref(doh->new, MKREF(dir_new));
341 lafs_dir_add_ent(buf, bits, name, nlen, target,
342 type, seed, doh->hash, doh->chainoffset);
343 lafs_dirty_dblock(doh->dirent_block);
344 if (dir->i_size <= blocksize) {
345 /* Make dir fit in inode if possible */
346 i_size_write(dir, lafs_dir_blk_size(buf, bits));
347 lafs_dirty_inode(dir);
349 clear_bit(B_PinPending, &doh->dirent_block->b.flags);
350 unmap_dblock(doh->dirent_block, buf);
351 putdref(doh->dirent_block, MKREF(dir_blk));
355 dir_create_pin(struct dirop_handle *doh)
358 err = lafs_pin_dblock(doh->dirent_block, NewSpace);
359 if (err || doh->new == NULL)
361 err = lafs_pin_dblock(doh->new, NewSpace);
366 dir_create_abort(struct dirop_handle *doh)
369 if (!IS_ERR(doh->dirent_block) && doh->dirent_block) {
370 clear_bit(B_PinPending, &doh->dirent_block->b.flags);
371 putdref(doh->dirent_block, MKREF(dir_blk));
373 if (!IS_ERR(doh->new) && doh->new) {
374 clear_bit(B_PinPending, &doh->new->b.flags);
375 putdref(doh->new, MKREF(dir_new));
379 /*---------------------------------------------------------------
380 * Delete directory entry.
381 * Deleting involves invalidating the entry in the dirent block,
382 * and then removing entry deleted entries that are not in a chain.
383 * If we cannot be sure, we schedule orphan processing to do
384 * the fine details of chain clearing.
387 dir_delete_prepare(struct fs *fs, struct inode *dir,
388 const char *name, int nlen, struct dirop_handle *doh)
390 struct datablock *dirblk;
394 dirblk = dir_lookup_blk(dir, name, nlen, &doh->index,
395 0, &doh->hash, MKREF(dir_blk));
396 if (IS_ERR(dirblk) && PTR_ERR(dirblk) == -ENOENT) {
398 dirblk = dir_lookup_blk(dir, name, nlen, &doh->index,
399 0, &doh->hash, MKREF(dir_blk));
401 printk("Weird: %s\n", strblk(&dirblk->b));
406 return PTR_ERR(dirblk);
407 lafs_iolock_written(&dirblk->b);
408 set_bit(B_PinPending, &dirblk->b.flags);
409 /* i_mutex protect us now, so don't need to maintain the lock */
410 lafs_iounlock_block(&dirblk->b);
412 /* Only make this block an orphan if there is a real
415 * We found the last possible entry
416 * We found the first entry
417 * We found the only entry (in which case we found the first)
418 * First entry is deleted
420 if (((doh->hash+1) & MaxDirHash) == doh->dirent_block->b.fileaddr)
421 /* Last possible entry is being remove */
424 u32 seed = LAFSI(dir)->md.file.seed;
427 char bits = dir->i_blkbits - 8;
428 char *buf = map_dblock(dirblk);
429 lafs_dir_find(buf, bits, seed, 0, &firstpiece);
430 if (doh->index == firstpiece ||
431 lafs_dir_extract(buf, bits, &de,
432 firstpiece, NULL)->target == 0)
434 unmap_dblock(dirblk, buf);
437 return lafs_make_orphan(fs, doh->dirent_block);
442 dir_delete_commit(struct dirop_handle *doh,
443 struct fs *fs, struct inode *dir,
444 const char *name, int nlen)
446 char *buf = map_dblock(doh->dirent_block);
447 char bits = dir->i_blkbits - 8;
449 u32 seed = LAFSI(dir)->md.file.seed;
452 /* First mark the entry as deleted, then consider removing it*/
455 lafs_dir_set_target(buf, bits, &de, doh->index);
457 /* If 'hash+1' is not in this block, make me an orphan
458 * (as we cannot check the chain)
459 * If it is and exists, do nothing (could be in active chain).
460 * If it doesn't exist:
461 * Remove this entry and any earlier deleted entries in a chain,
462 * but don't remove the first entry in the block.
463 * If we end up leaving that first entry, make me an orphan so
464 * the we can check if the chain continues in a previous block.
466 if (((doh->hash+1) & MaxDirHash) == doh->dirent_block->b.fileaddr)
467 unmap_dblock(doh->dirent_block, buf);
468 else if (lafs_dir_find(buf, bits, seed, doh->hash+1, &ignore) == 0) {
469 /* This is the end of a chain, clean up */
474 lafs_dir_find(buf, bits, seed, 0, &firstpiece);
475 hash = doh->hash; piece = doh->index;
477 if (piece == firstpiece)
479 lafs_dir_del_ent(buf, bits, seed, hash);
480 BUG_ON(hash == 0 || doh->hash - hash > 256);
482 } while (lafs_dir_find(buf, bits, seed, hash, &piece) &&
483 lafs_dir_extract(buf, bits, &de, piece,
486 unmap_dblock(doh->dirent_block, buf);
488 unmap_dblock(doh->dirent_block, buf);
490 lafs_dirty_dblock(doh->dirent_block);
491 clear_bit(B_PinPending, &doh->dirent_block->b.flags);
492 putdref(doh->dirent_block, MKREF(dir_blk));
496 dir_delete_pin(struct dirop_handle *doh)
499 err = lafs_pin_dblock(doh->dirent_block, ReleaseSpace);
506 dir_delete_abort(struct dirop_handle *doh)
508 if (doh->dirent_block &&
509 !IS_ERR(doh->dirent_block)) {
510 clear_bit(B_PinPending, &doh->dirent_block->b.flags);
511 putdref(doh->dirent_block, MKREF(dir_blk));
515 /*--------------------------------------------------------------
516 * Update directory entry
517 * This is used for rename when the target already exists
518 * Rather than delete+create it becomes delete+update
519 * This is even similar to delete except that we don't bother
523 dir_update_prepare(struct fs *fs, struct inode *dir,
524 const char *name, int nlen, struct dirop_handle *doh)
526 struct datablock *dirblk;
529 dirblk = dir_lookup_blk(dir, name, nlen, &doh->index,
530 0, NULL, MKREF(dir_blk));
532 return PTR_ERR(dirblk);
533 lafs_iolock_written(&dirblk->b);
534 set_bit(B_PinPending, &dirblk->b.flags);
535 /* i_mutex protect us now, so don't need to maintain the lock */
536 lafs_iounlock_block(&dirblk->b);
541 dir_update_commit(struct fs *fs, u32 target, int type,
542 struct dirop_handle *doh)
544 char *buf = map_dblock(doh->dirent_block);
545 int bits = doh->dirent_block->b.inode->i_blkbits - 8;
550 lafs_dir_set_target(buf, bits, &de, doh->index);
551 unmap_dblock(doh->dirent_block, buf);
552 lafs_dirty_dblock(doh->dirent_block);
553 clear_bit(B_PinPending, &doh->dirent_block->b.flags);
554 putdref(doh->dirent_block, MKREF(dir_blk));
558 dir_update_pin(struct dirop_handle *doh)
560 return lafs_pin_dblock(doh->dirent_block, ReleaseSpace);
564 dir_update_abort(struct dirop_handle *doh)
566 if (doh->dirent_block &&
567 !IS_ERR(doh->dirent_block)) {
568 clear_bit(B_PinPending, &doh->dirent_block->b.flags);
569 putdref(doh->dirent_block, MKREF(dir_blk));
573 /*------------------------------------------------------------
574 * Directory operations needs to be logged as transactions.
575 * The transaction is formed from the name preceded by 4 bytes
576 * for the inode number.
577 * A look to allow matching of related update is stored in the block number.
578 * The 'type' of transaction is recorded in the offset
581 static int dir_log_prepare(struct update_handle *uh,
585 return lafs_cluster_update_prepare(uh, fs, name->len+4);
588 static void dir_log_commit(struct update_handle *uh,
589 struct fs *fs, struct inode *dir,
590 struct qstr *name, u32 target,
591 int operation, u32 *handle)
602 case DIROP_REN_SOURCE:
603 while (++hancnt == 0)
608 case DIROP_REN_TARGET:
615 *(u32 *)mb = cpu_to_le32(target);
616 lafs_cluster_update_commit_buf(uh, fs, dir, han, operation,
618 name->len, name->name);
622 lafs_dir_roll_mini(struct inode *dir, int handle, int dirop,
623 u32 inum, char *name, int len)
626 struct dirop_handle doh, old_doh;
627 struct datablock *inodb = NULL, *olddb = NULL;
628 struct inode *inode = NULL;
629 struct rename_roll *rr = NULL, **rrp;
630 struct fs *fs = fs_from_inode(dir);
634 inode = lafs_iget(LAFSI(dir)->filesys, inum, SYNC);
636 return PTR_ERR(inode);
637 if (!inode && dirop != DIROP_REN_TARGET)
645 /* name doesn't exist - we create it. */
646 err = dir_create_prepare(fs, dir, name, len,
647 inum, mode_to_dt(inode->i_mode), &doh);
648 inodb = lafs_inode_dblock(dir, SYNC, MKREF(roll_dir));
650 err = PTR_ERR(inodb);
652 err = err ?: dir_create_pin(&doh);
653 err = err ?: lafs_pin_dblock(inodb, ReleaseSpace);
655 dir_create_abort(&doh);
658 inode_inc_link_count(inode);
659 lafs_inode_checkpin(inode);
660 lafs_dirty_dblock(inodb);
661 clear_bit(B_PinPending, &inodb->b.flags);
662 dir_create_commit(&doh, fs, dir, name, len,
663 inum, mode_to_dt(inode->i_mode));
668 /* Name exists, we need to remove it */
669 last = (inode->i_nlink == 1);
670 err = dir_delete_prepare(fs, dir, name, len, &doh);
671 inodb = lafs_inode_dblock(inode, SYNC, MKREF(roll_dir));
673 err = PTR_ERR(inodb);
675 err = lafs_make_orphan(fs, inodb);
677 dir_delete_abort(&doh);
680 lafs_iolock_block(&inodb->b);
681 set_bit(B_PinPending, &inodb->b.flags);
682 lafs_iounlock_block(&inodb->b);
683 err = dir_delete_pin(&doh);
684 err = err ?: lafs_pin_dblock(inodb, ReleaseSpace);
686 dir_delete_abort(&doh);
689 inode_dec_link_count(inode);
690 dir_delete_commit(&doh, fs, dir, name, len);
691 lafs_inode_checkpin(inode);
692 lafs_dirty_dblock(inodb);
693 clear_bit(B_PinPending, &inodb->b.flags);
697 case DIROP_REN_SOURCE:
698 rr = kmalloc(sizeof(*rr) + len, GFP_KERNEL);
703 rr->next = fs->pending_renames;
705 rr->dir = dir; igrab(dir);
706 rr->inode = inode; igrab(inode);
708 strncpy(rr->name, name, len);
710 fs->pending_renames = rr;
714 case DIROP_REN_TARGET:
715 rrp = &fs->pending_renames;
718 if (rr->key == handle)
730 last = (inode && inode->i_nlink == 1);
732 /* FIXME check both are dirs or non-dirs, and that a
733 * target directory is empty */
734 err = dir_delete_prepare(fs, rr->dir,
737 olddb = lafs_inode_dblock(rr->inode, SYNC, MKREF(roll_dir));
739 err = PTR_ERR(olddb);
741 /*unlink inode, update name */
742 err = dir_update_prepare(fs, dir, name, len, &doh)
744 inodb = lafs_inode_dblock(inode, SYNC, MKREF(roll_dir));
746 err = PTR_ERR(inodb);
748 err = lafs_make_orphan(fs, inodb);
749 lafs_iolock_block(&inodb->b);
750 set_bit(B_PinPending, &inodb->b.flags);
751 lafs_iounlock_block(&inodb->b);
753 /* create new link */
754 err = dir_create_prepare(fs, dir, name, len,
756 mode_to_dt(rr->inode->i_mode),
760 lafs_iolock_block(&olddb->b);
761 set_bit(B_PinPending, &olddb->b.flags);
762 lafs_iounlock_block(&olddb->b);
765 err = err ?: dir_delete_pin(&old_doh);
766 err = err ?: lafs_pin_dblock(olddb, ReleaseSpace);
768 err = err ?: lafs_pin_dblock(inodb, ReleaseSpace);
769 err = err ?: dir_update_pin(&doh);
771 err = err ?: dir_create_pin(&doh);
773 dir_delete_abort(&old_doh);
775 dir_update_abort(&doh);
777 dir_create_abort(&doh);
780 dir_delete_commit(&old_doh, fs, rr->dir, rr->name, rr->nlen);
781 if (S_ISDIR(rr->inode->i_mode)) {
782 inode_dec_link_count(rr->dir);
784 inode_inc_link_count(dir);
787 dir_update_commit(fs, rr->inode->i_ino,
788 mode_to_dt(rr->inode->i_mode),
791 dir_create_commit(&doh, fs, dir, name, len,
793 mode_to_dt(rr->inode->i_mode));
794 switch (LAFSI(rr->inode)->type) {
796 LAFSI(rr->inode)->md.file.parent = dir->i_ino;
799 LAFSI(rr->inode)->md.fs.parent = dir->i_ino;
803 if (S_ISDIR(inode->i_mode))
804 inode_dec_link_count(inode);
805 inode_dec_link_count(inode);
806 lafs_inode_checkpin(inode);
808 lafs_dirty_inode(rr->inode);
809 lafs_inode_checkpin(rr->dir);
810 lafs_inode_checkpin(dir);
811 clear_bit(B_PinPending, &olddb->b.flags);
813 clear_bit(B_PinPending, &inodb->b.flags);
814 putdref(inodb, MKREF(dir_roll));
819 if (inode && !IS_ERR(inode))
821 if (inodb && !IS_ERR(inodb))
822 putdref(inodb, MKREF(roll_dir));
823 if (olddb && !IS_ERR(olddb))
824 putdref(olddb, MKREF(roll_dir));
832 /*------------------------------------------------------------
833 * Now we have the lowlevel operations in place, we
834 * can implement the VFS interface.
837 lafs_create(struct inode *dir, struct dentry *de, int mode,
838 struct nameidata *nd)
840 /* Need to allocate an inode and space in the directory */
841 struct fs *fs = fs_from_inode(dir);
842 struct datablock *db;
843 struct inode *ino = lafs_new_inode(fs, LAFSI(dir)->filesys,
844 dir, TypeFile, 0, mode, &db);
845 struct dirop_handle doh;
846 struct update_handle uh;
852 err = dir_create_prepare(fs, dir, de->d_name.name, de->d_name.len,
853 ino->i_ino, DT_REG, &doh);
855 dprintk("ERR = %d\n", err);
856 err = dir_log_prepare(&uh, fs, &de->d_name) ?: err;
857 dprintk("ERR2 = %d\n", err);
862 dprintk("lc: dirblk = %p\n", doh.dirent_block);
863 lafs_checkpoint_lock(fs);
865 err = dir_create_pin(&doh);
866 err = err ?: lafs_cluster_update_pin(&uh);
867 err = err ?: lafs_pin_dblock(db, NewSpace);
868 if (err == -EAGAIN) {
869 lafs_checkpoint_unlock_wait(fs);
876 lafs_add_orphan(fs, db);
877 LAFSI(ino)->md.file.parent = dir->i_ino;
878 lafs_dirty_inode(ino);
879 lafs_inode_checkpin(ino);
880 dir_log_commit(&uh, fs, dir, &de->d_name, ino->i_ino, DIROP_LINK, NULL);
881 dir_create_commit(&doh, fs, dir, de->d_name.name, de->d_name.len,
883 lafs_checkpoint_unlock(fs);
884 d_instantiate(de, ino);
885 clear_bit(B_PinPending, &db->b.flags);
886 putdref(db, MKREF(inode_new));
890 lafs_checkpoint_unlock(fs);
892 lafs_cluster_update_abort(&uh);
893 dir_create_abort(&doh);
895 clear_bit(B_PinPending, &db->b.flags);
896 putdref(db, MKREF(inode_new));
901 lafs_link(struct dentry *from, struct inode *dir, struct dentry *to)
903 /* Create the new name and increase the link count on the target */
904 struct fs *fs = fs_from_inode(dir);
905 struct dirop_handle doh;
906 struct update_handle uh;
907 struct inode *inode = from->d_inode;
908 struct datablock *inodb;
911 if (inode->i_nlink >= LAFS_MAX_LINKS)
913 err = dir_create_prepare(fs, dir, to->d_name.name, to->d_name.len,
914 inode->i_ino, mode_to_dt(inode->i_mode),
916 err = dir_log_prepare(&uh, fs, &to->d_name) ?: err;
918 inodb = lafs_inode_dblock(dir, SYNC, MKREF(link));
920 err = PTR_ERR(inodb);
924 lafs_checkpoint_lock(fs);
926 err = dir_create_pin(&doh);
927 err = err ?: lafs_cluster_update_pin(&uh);
928 err = err ?: lafs_pin_dblock(inodb, NewSpace);
929 if (err == -EAGAIN) {
930 lafs_checkpoint_unlock_wait(fs);
936 inode_inc_link_count(inode);
937 lafs_inode_checkpin(inode);
938 lafs_dirty_dblock(inodb);
939 clear_bit(B_PinPending, &inodb->b.flags);
940 putdref(inodb, MKREF(link));
942 dir_log_commit(&uh, fs, dir, &to->d_name, inode->i_ino,
944 dir_create_commit(&doh, fs, dir, to->d_name.name, to->d_name.len,
945 inode->i_ino, mode_to_dt(inode->i_mode));
946 /* Don't log the nlink change - that is implied in the name creation */
947 d_instantiate(to, inode);
949 lafs_checkpoint_unlock(fs);
952 lafs_checkpoint_unlock(fs);
953 clear_bit(B_PinPending, &inodb->b.flags);
956 putdref(inodb, MKREF(link));
957 dir_create_abort(&doh);
958 lafs_cluster_update_abort(&uh);
963 lafs_unlink(struct inode *dir, struct dentry *de)
965 struct fs *fs = fs_from_inode(dir);
966 struct inode *inode = de->d_inode;
967 int last = (inode->i_nlink == 1);
968 struct dirop_handle doh;
969 struct update_handle uh;
970 struct datablock *inodb;
973 dprintk("unlink %s\n", de->d_name.name);
975 err = dir_delete_prepare(fs, dir, de->d_name.name, de->d_name.len,
977 BUG_ON(err == -ENOENT);
978 err = dir_log_prepare(&uh, fs, &de->d_name) ?: err;
979 inodb = lafs_inode_dblock(inode, SYNC, MKREF(inode_update));
981 err = PTR_ERR(inodb);
983 err = lafs_make_orphan(fs, inodb);
986 lafs_iolock_block(&inodb->b);
987 set_bit(B_PinPending, &inodb->b.flags);
988 lafs_iounlock_block(&inodb->b);
990 lafs_checkpoint_lock(fs);
992 err = dir_delete_pin(&doh);
993 err = err ?: lafs_cluster_update_pin(&uh);
994 err = err ?: lafs_pin_dblock(inodb, ReleaseSpace);
995 if (err == -EAGAIN) {
996 lafs_checkpoint_unlock_wait(fs);
1002 inode_dec_link_count(inode);
1003 dir_log_commit(&uh, fs, dir, &de->d_name, inode->i_ino,
1004 DIROP_UNLINK, NULL);
1005 dir_delete_commit(&doh, fs, dir, de->d_name.name, de->d_name.len);
1006 lafs_checkpoint_unlock(fs);
1007 lafs_inode_checkpin(inode);
1008 lafs_dirty_dblock(inodb);
1009 clear_bit(B_PinPending, &inodb->b.flags);
1010 putdref(inodb, MKREF(inode_update));
1013 clear_bit(B_PinPending, &inodb->b.flags);
1014 lafs_checkpoint_unlock(fs);
1017 putdref(inodb, MKREF(inode_update));
1018 lafs_cluster_update_abort(&uh);
1019 dir_delete_abort(&doh);
1023 static void dir_flush_orphans(struct fs *fs, struct inode *inode)
1026 * Orphans cannot clear while we hold i_mutex, so
1027 * we have to run them ourselves.
1029 struct datablock *db;
1031 while ((db = lafs_find_orphan(inode))) {
1033 prepare_to_wait(&fs->async_complete, &wq,
1034 TASK_UNINTERRUPTIBLE);
1035 getdref(db, MKREF(rmdir_orphan));
1036 lafs_dir_handle_orphan(db);
1037 still_orphan = test_bit(B_Orphan, &db->b.flags);
1038 putdref(db, MKREF(rmdir_orphan));
1040 /* still an orphan, need to wait */
1043 finish_wait(&fs->async_complete, &wq);
1047 lafs_rmdir(struct inode *dir, struct dentry *de)
1049 struct fs *fs = fs_from_inode(dir);
1050 struct inode *inode = de->d_inode;
1051 struct dirop_handle doh;
1052 struct update_handle uh;
1053 struct datablock *inodb;
1056 if (inode->i_nlink > 2)
1058 if (inode->i_size) {
1059 /* Probably not empty, but it could be that we
1060 * just need to wait for orphans the clear.
1062 dir_flush_orphans(fs, inode);
1067 dprintk("rmdir %s\n", de->d_name.name);
1069 err = dir_delete_prepare(fs, dir, de->d_name.name, de->d_name.len,
1071 err = dir_log_prepare(&uh, fs, &de->d_name) ?: err;
1072 inodb = lafs_inode_dblock(inode, SYNC, MKREF(inode_update));
1074 err = PTR_ERR(inodb);
1076 err = lafs_make_orphan(fs, inodb);
1079 lafs_iolock_block(&inodb->b);
1080 set_bit(B_PinPending, &inodb->b.flags);
1081 lafs_iounlock_block(&inodb->b);
1083 lafs_checkpoint_lock(fs);
1085 err = dir_delete_pin(&doh);
1086 err = err ?: lafs_cluster_update_pin(&uh);
1087 err = err ?: lafs_pin_dblock(inodb, ReleaseSpace);
1088 if (err == -EAGAIN) {
1089 lafs_checkpoint_unlock_wait(fs);
1095 inode_dec_link_count(dir);
1096 inode_dec_link_count(inode); /* . */
1097 inode_dec_link_count(inode); /* .. */
1098 dir_log_commit(&uh, fs, dir, &de->d_name, inode->i_ino,
1099 DIROP_UNLINK, NULL);
1100 dir_delete_commit(&doh, fs, dir, de->d_name.name, de->d_name.len);
1101 lafs_dirty_dblock(inodb);
1102 clear_bit(B_PinPending, &inodb->b.flags);
1103 putdref(inodb, MKREF(inode_update));
1104 lafs_inode_checkpin(inode);
1105 lafs_inode_checkpin(dir);
1106 lafs_checkpoint_unlock(fs);
1109 lafs_checkpoint_unlock(fs);
1110 clear_bit(B_PinPending, &inodb->b.flags);
1113 putdref(inodb, MKREF(inode_update));
1114 lafs_cluster_update_abort(&uh);
1115 dir_delete_abort(&doh);
1120 lafs_symlink(struct inode *dir, struct dentry *de,
1121 const char *symlink)
1125 struct fs *fs = fs_from_inode(dir);
1126 struct datablock *b, *inodb;
1127 struct dirop_handle doh;
1128 struct update_handle uh;
1132 l = strlen(symlink);
1133 if (l > fs->blocksize-1)
1134 return -ENAMETOOLONG;
1136 ino = lafs_new_inode(fs, LAFSI(dir)->filesys, dir,
1137 TypeSymlink, 0, 0666, &inodb);
1139 return PTR_ERR(ino);
1140 b = lafs_get_block(ino, 0, NULL, GFP_KERNEL, MKREF(symlink));
1142 putdref(inodb, MKREF(inode_new));
1146 set_bit(B_PinPending, &b->b.flags);
1148 err = dir_create_prepare(fs, dir, de->d_name.name, de->d_name.len,
1149 ino->i_ino, DT_LNK, &doh);
1150 err = dir_log_prepare(&uh, fs, &de->d_name) ?: err;
1154 lafs_checkpoint_lock(fs);
1156 err = dir_create_pin(&doh);
1157 err = err ?: lafs_pin_dblock(b, NewSpace);
1158 err = err ?: lafs_cluster_update_pin(&uh);
1159 if (err == -EAGAIN) {
1160 lafs_checkpoint_unlock_wait(fs);
1167 LAFSI(ino)->md.file.parent = dir->i_ino;
1168 lafs_add_orphan(fs, inodb);
1170 lafs_iolock_block(&b->b);
1171 buf = map_dblock(b);
1172 memcpy(buf, symlink, l);
1174 unmap_dblock(b, buf);
1175 set_bit(B_Valid, &b->b.flags);
1176 lafs_dirty_dblock(b);
1177 clear_bit(B_PinPending, &b->b.flags);
1178 lafs_cluster_allocate(&b->b, 0); /* Content will go in the next cluster - almost like
1180 putdref(b, MKREF(symlink));
1181 i_size_write(ino, l);
1182 lafs_dirty_inode(ino);
1183 lafs_inode_checkpin(ino);
1185 dir_log_commit(&uh, fs, dir, &de->d_name, ino->i_ino, DIROP_LINK, NULL);
1186 dir_create_commit(&doh, fs, dir, de->d_name.name, de->d_name.len,
1187 ino->i_ino, DT_LNK);
1188 lafs_checkpoint_unlock(fs);
1189 d_instantiate(de, ino);
1190 putdref(inodb, MKREF(inode_new));
1193 lafs_checkpoint_unlock(fs);
1195 putdref(inodb, MKREF(inode_new));
1196 clear_bit(B_PinPending, &b->b.flags);
1197 putdref(b, MKREF(symlink));
1198 dir_create_abort(&doh);
1199 lafs_cluster_update_abort(&uh);
1205 lafs_mkdir(struct inode *dir, struct dentry *de, int mode)
1208 struct lafs_inode *lai;
1209 struct fs *fs = fs_from_inode(dir);
1211 struct dirop_handle doh;
1212 struct update_handle uh;
1213 struct datablock *inodb;
1215 if (dir->i_nlink >= LAFS_MAX_LINKS)
1218 ino = lafs_new_inode(fs, LAFSI(dir)->filesys, dir,
1219 TypeDir, 0, mode, &inodb);
1221 return PTR_ERR(ino);
1223 err = dir_create_prepare(fs, dir, de->d_name.name, de->d_name.len,
1224 ino->i_ino, DT_DIR, &doh);
1225 err = dir_log_prepare(&uh, fs, &de->d_name) ?: err;
1229 lafs_checkpoint_lock(fs);
1231 err = dir_create_pin(&doh);
1232 err = err ?: lafs_pin_dblock(inodb, NewSpace);
1233 err = err ?: lafs_cluster_update_pin(&uh);
1234 if (err == -EAGAIN) {
1235 lafs_checkpoint_unlock_wait(fs);
1242 lai->md.file.parent = dir->i_ino;
1243 inode_inc_link_count(dir);
1244 ino->i_nlink = 2; /* From parent, and from '.' */
1245 lafs_dirty_inode(ino);
1246 lafs_inode_checkpin(dir);
1247 lafs_inode_checkpin(ino);
1248 lafs_add_orphan(fs, inodb);
1249 dir_create_commit(&doh, fs, dir, de->d_name.name, de->d_name.len,
1250 ino->i_ino, DT_DIR);
1251 d_instantiate(de, ino);
1252 clear_bit(B_PinPending, &inodb->b.flags);
1253 putdref(inodb, MKREF(inode_new));
1254 lafs_checkpoint_unlock(fs);
1257 lafs_checkpoint_unlock(fs);
1259 dir_create_abort(&doh);
1260 lafs_cluster_update_abort(&uh);
1262 clear_bit(B_PinPending, &inodb->b.flags);
1263 putdref(inodb, MKREF(inode_new));
1268 lafs_mknod(struct inode *dir, struct dentry *de, int mode,
1272 struct fs *fs = fs_from_inode(dir);
1274 struct dirop_handle doh;
1275 struct update_handle uh;
1276 struct datablock *inodb;
1279 if (!new_valid_dev(rdev))
1283 switch (mode & S_IFMT) {
1295 ino = lafs_new_inode(fs, LAFSI(dir)->filesys, dir,
1296 type, 0, mode, &inodb);
1298 return PTR_ERR(ino);
1299 init_special_inode(ino, ino->i_mode, rdev);
1301 err = dir_create_prepare(fs, dir, de->d_name.name, de->d_name.len,
1302 ino->i_ino, type, &doh);
1303 err = dir_log_prepare(&uh, fs, &de->d_name) ?: err;
1307 lafs_checkpoint_lock(fs);
1309 err = dir_create_pin(&doh);
1310 err = err ?: lafs_pin_dblock(inodb, NewSpace);
1311 err = err ?: lafs_cluster_update_pin(&uh);
1312 if (err == -EAGAIN) {
1313 lafs_checkpoint_unlock_wait(fs);
1319 LAFSI(ino)->md.file.parent = dir->i_ino;
1321 lafs_add_orphan(fs, inodb);
1322 lafs_dirty_inode(ino);
1323 lafs_inode_checkpin(ino);
1325 dir_log_commit(&uh, fs, dir, &de->d_name, ino->i_ino, DIROP_LINK, NULL);
1326 dir_create_commit(&doh, fs, dir, de->d_name.name, de->d_name.len,
1328 lafs_checkpoint_unlock(fs);
1329 d_instantiate(de, ino);
1330 clear_bit(B_PinPending, &inodb->b.flags);
1331 putdref(inodb, MKREF(inode_new));
1334 lafs_checkpoint_unlock(fs);
1336 dir_create_abort(&doh);
1337 lafs_cluster_update_abort(&uh);
1339 clear_bit(B_PinPending, &inodb->b.flags);
1340 putdref(inodb, MKREF(inode_new));
1345 lafs_rename(struct inode *old_dir, struct dentry *old_dentry,
1346 struct inode *new_dir, struct dentry *new_dentry)
1348 /* VFS has checked that this is file->file or dir->dir or
1349 * something->nothing.
1350 * We just need to check that if the target is a directory,
1351 * it is empty, then perform the rename
1353 struct fs *fs = fs_from_inode(old_dir);
1355 struct inode *old_inode = old_dentry->d_inode;
1356 struct inode *new_inode = new_dentry->d_inode;
1358 struct datablock *olddb, *newdb = newdb;
1360 struct dirop_handle old_doh, new_doh;
1361 struct update_handle old_uh, new_uh;
1362 int last = (new_inode && new_inode->i_nlink == 1);
1366 if (S_ISDIR(old_inode->i_mode)) {
1368 if (new_inode->i_size) {
1369 dir_flush_orphans(fs, new_inode);
1370 if (new_inode->i_size)
1373 } else if (new_dir != old_dir) {
1374 /* New dir is getting a new link */
1375 if (new_dir->i_nlink >= LAFS_MAX_LINKS)
1379 dprintk("rename %s %s\n", old_dentry->d_name.name,
1380 new_dentry->d_name.name);
1382 /* old entry gets deleted, new entry gets created or updated. */
1383 err = dir_delete_prepare(fs, old_dir,
1384 old_dentry->d_name.name,
1385 old_dentry->d_name.len,
1387 err = dir_log_prepare(&old_uh, fs, &old_dentry->d_name) ?: err;
1388 err = dir_log_prepare(&new_uh, fs, &new_dentry->d_name) ?: err;
1389 olddb = lafs_inode_dblock(old_inode, SYNC, MKREF(inode_update));
1391 err = PTR_ERR(olddb);
1394 /* unlink object, update name */
1395 err = dir_update_prepare(fs, new_dir,
1396 new_dentry->d_name.name,
1397 new_dentry->d_name.len,
1399 newdb = lafs_inode_dblock(new_inode, SYNC, MKREF(inode_update));
1401 err = PTR_ERR(newdb);
1403 err = lafs_make_orphan(fs, newdb);
1404 lafs_iolock_block(&newdb->b);
1405 set_bit(B_PinPending, &newdb->b.flags);
1406 lafs_iounlock_block(&newdb->b);
1408 /* create new link */
1409 err = dir_create_prepare(fs, new_dir,
1410 new_dentry->d_name.name,
1411 new_dentry->d_name.len,
1413 mode_to_dt(old_inode->i_mode),
1419 lafs_iolock_block(&olddb->b);
1420 set_bit(B_PinPending, &olddb->b.flags);
1421 lafs_iounlock_block(&olddb->b);
1423 lafs_checkpoint_lock(fs);
1425 err = dir_delete_pin(&old_doh);
1426 err = err ?: lafs_cluster_update_pin(&old_uh);
1427 err = err ?: lafs_cluster_update_pin(&new_uh);
1428 err = err ?: lafs_pin_dblock(olddb, ReleaseSpace);
1430 err = err ?: lafs_pin_dblock(newdb, NewSpace);
1431 err = err ?: dir_update_pin(&new_doh);
1433 err = err ?: dir_create_pin(&new_doh);
1435 if (err == -EAGAIN) {
1436 lafs_checkpoint_unlock_wait(fs);
1442 /* OK, let's do the deed */
1443 dir_delete_commit(&old_doh, fs, old_dir, old_dentry->d_name.name,
1444 old_dentry->d_name.len);
1445 dir_log_commit(&old_uh, fs, old_dir, &old_dentry->d_name,
1446 old_inode->i_ino, DIROP_REN_SOURCE, &renhandle);
1447 dir_log_commit(&new_uh, fs, new_dir, &new_dentry->d_name,
1448 new_inode ? new_inode->i_ino : 0,
1451 if (S_ISDIR(old_inode->i_mode)) {
1452 inode_dec_link_count(old_dir);
1454 inode_inc_link_count(new_dir);
1457 dir_update_commit(fs, old_inode->i_ino,
1458 mode_to_dt(old_inode->i_mode),
1461 dir_create_commit(&new_doh, fs, new_dir,
1462 new_dentry->d_name.name,
1463 new_dentry->d_name.len,
1465 mode_to_dt(old_inode->i_mode));
1466 switch (LAFSI(old_inode)->type) {
1468 LAFSI(old_inode)->md.file.parent = new_dir->i_ino;
1471 LAFSI(old_inode)->md.fs.parent = new_dir->i_ino;
1475 if (S_ISDIR(new_inode->i_mode))
1476 inode_dec_link_count(new_inode);
1477 inode_dec_link_count(new_inode);
1478 lafs_inode_checkpin(new_inode);
1480 lafs_dirty_inode(old_inode);
1481 lafs_inode_checkpin(new_dir);
1482 lafs_inode_checkpin(old_dir);
1483 clear_bit(B_PinPending, &olddb->b.flags);
1484 putdref(olddb, MKREF(inode_update));
1486 clear_bit(B_PinPending, &newdb->b.flags);
1487 putdref(newdb, MKREF(inode_new));
1490 lafs_checkpoint_unlock(fs);
1494 lafs_checkpoint_unlock(fs);
1495 clear_bit(B_PinPending, &olddb->b.flags);
1497 clear_bit(B_PinPending, &newdb->b.flags);
1499 dir_delete_abort(&old_doh);
1500 lafs_cluster_update_abort(&old_uh);
1501 lafs_cluster_update_abort(&new_uh);
1503 putdref(olddb, MKREF(inode_update));
1505 dir_update_abort(&new_doh);
1507 putdref(newdb, MKREF(inode_new));
1509 dir_create_abort(&new_doh);
1513 /*--------------------------------------------------------------------
1514 * Directory Orphan handling.
1516 * blocks in a directory file that are 'orphans' have recently had a deletion
1518 * - to be punched as a hole if empty
1519 * - to have 'deleted' entries purged in they are freeable
1520 * - to schedule next block for orphan handling if that might be appropriate.
1524 * Lock the directory.
1525 * If last possible entry (addr-1) is deleted,
1526 * look for next entry.
1527 * If it doesn't exist, remove last entry an preceding deleted
1528 * entries, just like with delete.
1529 * If first block is 'deleted' and next is removed,
1530 * remove that deleted entry.
1531 * look for previous entry.
1532 * if it is deleted, schedule orphan handling.
1533 * If ->root is 0, punch a hole
1536 int lafs_dir_handle_orphan(struct datablock *b)
1538 struct inode *dir = b->b.inode;
1539 struct fs *fs = fs_from_inode(dir);
1540 int bits = dir->i_blkbits-8;
1541 u32 seed = LAFSI(dir)->md.file.seed;
1544 struct datablock *b2 = NULL;
1545 u8 piece, firstpiece;
1549 dprintk("HANDLE ORPHAN h=%x %s\n", (unsigned)hash, strblk(&b->b));
1551 if (!lafs_iolock_written_async(&b->b))
1553 set_bit(B_PinPending, &b->b.flags);
1554 lafs_iounlock_block(&b->b);
1556 lafs_checkpoint_lock(fs);
1558 if (!test_bit(B_Valid, &b->b.flags)) {
1559 /* probably have already erased this block,
1560 * but the orphan_release failed due to
1561 * space being tight.
1564 lafs_orphan_release(fs, b);
1569 /* First test: Does a chain of deleted entries extend beyond
1570 * the end of this block. i.e. is the last entry deleted.
1571 * If so, look at the next block and see if the chain is still
1572 * anchored, or if it can all be released.
1574 buf = map_dblock(b);
1575 hash = (b->b.fileaddr-1) & MaxDirHash;
1576 if (lafs_dir_find(buf, bits, seed, hash, &piece) &&
1577 lafs_dir_extract(buf, bits, &de, piece, NULL)->target == 0) {
1579 unmap_dblock(b, buf);
1580 bnum = b->b.fileaddr + 1;
1581 if (lafs_find_next(dir, &bnum) == 0)
1582 /* FIXME what if it returns an error */
1585 b2 = lafs_get_block(dir, bnum, NULL, GFP_KERNEL,
1590 err = lafs_read_block_async(b2);
1594 buf2 = map_dblock(b2);
1595 if (lafs_dir_find(buf2, bits, seed, hash+1, &piece) == 0) {
1597 /* We can remove that last entry, and maybe others */
1598 unmap_dblock(b2, buf2);
1600 err = lafs_pin_dblock(b, ReleaseSpace);
1603 buf = map_dblock(b);
1604 lafs_dir_find(buf, bits, seed, 0, &firstpiece);
1606 if (piece == firstpiece)
1608 lafs_dir_del_ent(buf, bits, seed, hash);
1610 } while (lafs_dir_find(buf, bits, seed, hash, &piece) &&
1611 lafs_dir_extract(buf, bits, &de, piece,
1612 NULL)->target == 0);
1613 unmap_dblock(b, buf);
1614 lafs_dirty_dblock(b);
1616 unmap_dblock(b2, buf2);
1617 buf = map_dblock(b);
1618 putdref(b2, MKREF(dir_orphan));
1622 /* Second test: if we have an unanchored chain at the start
1623 * of the block, then schedule orphan handling for previous block,
1624 * and remove the unanchor.
1626 lafs_dir_find(buf, bits, seed, 0, &firstpiece);
1629 lafs_dir_extract(buf, bits, &de, firstpiece, &hash)->target == 0 &&
1630 lafs_dir_find(buf, bits, seed, hash+1, &piece) == 0) {
1631 unmap_dblock(b, buf);
1632 b2 = lafs_get_block(dir, hash, NULL, GFP_KERNEL,
1637 err = lafs_read_block_async(b2);
1641 buf2 = map_dblock(b2);
1642 if (lafs_dir_find(buf2, bits, seed, (hash-1) & MaxDirHash,
1644 lafs_dir_extract(buf2, bits, &de, piece, NULL)->target == 0)
1645 err = lafs_make_orphan_nb(fs, b2);
1646 unmap_dblock(b2, buf2);
1647 putdref(b2, MKREF(dir_orphan));
1651 err = lafs_pin_dblock(b, ReleaseSpace);
1654 buf = map_dblock(b);
1655 lafs_dir_del_ent(buf, bits, seed, hash);
1656 lafs_dirty_dblock(b);
1659 if (lafs_dir_empty(buf)) {
1661 unmap_dblock(b, buf);
1663 err = lafs_pin_dblock(b, ReleaseSpace);
1668 err = lafs_find_next(dir, &bnum);
1672 if (b->b.fileaddr == 0)
1673 i_size_write(dir, 0);
1675 b2 = lafs_get_block(dir, 0, NULL, GFP_KERNEL,
1680 err = lafs_read_block_async(b2);
1683 buf2 = map_dblock(b2);
1685 lafs_dir_blk_size(buf2, bits));
1686 unmap_dblock(b2, buf2);
1687 putdref(b2, MKREF(dir_orphan));
1690 lafs_dirty_inode(dir);
1692 lafs_erase_dblock(b);
1694 unmap_dblock(b, buf);
1696 lafs_orphan_release(fs, b);
1700 clear_bit(B_PinPending, &b->b.flags);
1701 putdref(b2, MKREF(dir_orphan));
1702 lafs_checkpoint_unlock(fs);
1706 /*--------------------------------------------------------------------
1707 * Finally the read-only operations
1710 lafs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1712 struct dentry *dentry = filp->f_dentry;
1713 struct lafs_inode *lai = LAFSI(dentry->d_inode);
1715 loff_t i = filp->f_pos;
1720 int bits = dentry->d_inode->i_blkbits - 8;
1721 u32 seed = lai->md.file.seed;
1725 ino = dentry->d_inode->i_ino;
1726 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1732 ino = lai->md.file.parent;
1733 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1743 struct datablock *b;
1747 switch (lafs_find_next(dentry->d_inode, &bnum)) {
1756 b = lafs_get_block(dentry->d_inode, bnum, NULL,
1757 GFP_KERNEL, MKREF(readdir));
1762 err = lafs_read_block(b);
1765 /* buf = map_dblock(b); */
1766 buf = kmap(b->page);
1767 buf += dblock_offset(b);
1772 lafs_dir_find(buf, bits, seed, hash, &piece);
1776 lafs_dir_extract(buf, bits, &de,
1779 if (de.target == 0) {
1781 filp->f_pos = hash+2;
1784 /* This is a good name to return */
1785 over = filldir(dirent, de.name, de.nlen,
1786 hash+2, de.target, de.type);
1789 filp->f_pos = hash+2;
1793 /* unmap_dblock(b, buf); */
1795 putdref(b, MKREF(readdir));
1797 } while (bnum && !over);
1803 static struct dentry *
1804 lafs_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1806 /* Simple lookup that maps inode number to that inode. */
1809 int err = dir_lookup(dir, dentry->d_name.name, dentry->d_name.len,
1812 if (err == -ENOENT) {
1813 d_add(dentry, NULL);
1816 /* FIXME range check inum */
1819 return ERR_PTR(err);
1820 ino = lafs_iget(LAFSI(dir)->filesys, inum, SYNC);
1823 return ERR_PTR(PTR_ERR(ino));
1824 return d_splice_alias(ino, dentry);
1827 static int lafs_getattr_dir(struct vfsmount *mnt, struct dentry *dentry,
1830 lafs_fillattr(dentry->d_inode, stat);
1831 /* hide 'holes' in directories by making the size match
1832 * the number of allocated blocks.
1834 if (stat->size > dentry->d_inode->i_sb->s_blocksize)
1835 stat->size = (dentry->d_inode->i_sb->s_blocksize *
1836 (LAFSI(dentry->d_inode)->cblocks +
1837 LAFSI(dentry->d_inode)->pblocks +
1838 LAFSI(dentry->d_inode)->ablocks));
1842 const struct file_operations lafs_dir_file_operations = {
1843 .llseek = generic_file_llseek, /* Just set 'pos' */
1844 .read = generic_read_dir, /* return error */
1845 .readdir = lafs_readdir,
1847 .fsync = lafs_sync_cluster,
1851 const struct inode_operations lafs_dir_ino_operations = {
1852 .lookup = lafs_lookup,
1853 .create = lafs_create,
1855 .unlink = lafs_unlink,
1856 .symlink = lafs_symlink,
1857 .mkdir = lafs_mkdir,
1858 .rmdir = lafs_rmdir,
1859 .rename = lafs_rename,
1860 .mknod = lafs_mknod,
1861 .setattr = lafs_setattr,
1862 .getattr = lafs_getattr_dir,