xref: /openbmc/linux/fs/reiserfs/dir.c (revision 4b4193256c8d3bc3a5397b5cd9494c2ad386317d)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
31da177e4SLinus Torvalds  */
41da177e4SLinus Torvalds 
51da177e4SLinus Torvalds #include <linux/string.h>
61da177e4SLinus Torvalds #include <linux/errno.h>
71da177e4SLinus Torvalds #include <linux/fs.h>
8f466c6fdSAl Viro #include "reiserfs.h"
91da177e4SLinus Torvalds #include <linux/stat.h>
101da177e4SLinus Torvalds #include <linux/buffer_head.h>
115a0e3ad6STejun Heo #include <linux/slab.h>
1217093991SFabian Frederick #include <linux/uaccess.h>
131da177e4SLinus Torvalds 
145a1b6391SDavid Woodhouse extern const struct reiserfs_key MIN_KEY;
151da177e4SLinus Torvalds 
164acf381eSAl Viro static int reiserfs_readdir(struct file *, struct dir_context *);
1702c24a82SJosef Bacik static int reiserfs_dir_fsync(struct file *filp, loff_t start, loff_t end,
1802c24a82SJosef Bacik 			      int datasync);
191da177e4SLinus Torvalds 
204b6f5d20SArjan van de Ven const struct file_operations reiserfs_dir_operations = {
21ca572727Sjan Blunck 	.llseek = generic_file_llseek,
221da177e4SLinus Torvalds 	.read = generic_read_dir,
23c51da20cSAl Viro 	.iterate_shared = reiserfs_readdir,
241da177e4SLinus Torvalds 	.fsync = reiserfs_dir_fsync,
25205cb37bSFrederic Weisbecker 	.unlocked_ioctl = reiserfs_ioctl,
2652b499c4SDavid Howells #ifdef CONFIG_COMPAT
2752b499c4SDavid Howells 	.compat_ioctl = reiserfs_compat_ioctl,
2852b499c4SDavid Howells #endif
291da177e4SLinus Torvalds };
301da177e4SLinus Torvalds 
reiserfs_dir_fsync(struct file * filp,loff_t start,loff_t end,int datasync)3102c24a82SJosef Bacik static int reiserfs_dir_fsync(struct file *filp, loff_t start, loff_t end,
3202c24a82SJosef Bacik 			      int datasync)
33bd4c625cSLinus Torvalds {
347ea80859SChristoph Hellwig 	struct inode *inode = filp->f_mapping->host;
351da177e4SLinus Torvalds 	int err;
3602c24a82SJosef Bacik 
373b49c9a1SJeff Layton 	err = file_write_and_wait_range(filp, start, end);
3802c24a82SJosef Bacik 	if (err)
3902c24a82SJosef Bacik 		return err;
4002c24a82SJosef Bacik 
415955102cSAl Viro 	inode_lock(inode);
421da177e4SLinus Torvalds 	reiserfs_write_lock(inode->i_sb);
431da177e4SLinus Torvalds 	err = reiserfs_commit_for_inode(inode);
441da177e4SLinus Torvalds 	reiserfs_write_unlock(inode->i_sb);
455955102cSAl Viro 	inode_unlock(inode);
461da177e4SLinus Torvalds 	if (err < 0)
471da177e4SLinus Torvalds 		return err;
481da177e4SLinus Torvalds 	return 0;
491da177e4SLinus Torvalds }
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds #define store_ih(where,what) copy_item_head (where, what)
521da177e4SLinus Torvalds 
is_privroot_deh(struct inode * dir,struct reiserfs_de_head * deh)5399ce4169SAl Viro static inline bool is_privroot_deh(struct inode *dir, struct reiserfs_de_head *deh)
54677c9b2eSJeff Mahoney {
5599ce4169SAl Viro 	struct dentry *privroot = REISERFS_SB(dir->i_sb)->priv_root;
562b0143b5SDavid Howells 	return (d_really_is_positive(privroot) &&
572b0143b5SDavid Howells 	        deh->deh_objectid == INODE_PKEY(d_inode(privroot))->k_objectid);
58677c9b2eSJeff Mahoney }
59677c9b2eSJeff Mahoney 
reiserfs_readdir_inode(struct inode * inode,struct dir_context * ctx)60cd62cdaeSAl Viro int reiserfs_readdir_inode(struct inode *inode, struct dir_context *ctx)
611da177e4SLinus Torvalds {
62098297b2SJeff Mahoney 
63098297b2SJeff Mahoney 	/* key of current position in the directory (key of directory entry) */
64098297b2SJeff Mahoney 	struct cpu_key pos_key;
65098297b2SJeff Mahoney 
661da177e4SLinus Torvalds 	INITIALIZE_PATH(path_to_entry);
671da177e4SLinus Torvalds 	struct buffer_head *bh;
681da177e4SLinus Torvalds 	int item_num, entry_num;
691da177e4SLinus Torvalds 	const struct reiserfs_key *rkey;
701da177e4SLinus Torvalds 	struct item_head *ih, tmp_ih;
711da177e4SLinus Torvalds 	int search_res;
721da177e4SLinus Torvalds 	char *local_buf;
731da177e4SLinus Torvalds 	loff_t next_pos;
741da177e4SLinus Torvalds 	char small_buf[32];	/* avoid kmalloc if we can */
751da177e4SLinus Torvalds 	struct reiserfs_dir_entry de;
761da177e4SLinus Torvalds 	int ret = 0;
77278f6679SJeff Mahoney 	int depth;
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds 	reiserfs_write_lock(inode->i_sb);
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds 	reiserfs_check_lock_depth(inode->i_sb, "readdir");
821da177e4SLinus Torvalds 
83098297b2SJeff Mahoney 	/*
84098297b2SJeff Mahoney 	 * form key for search the next directory entry using
85098297b2SJeff Mahoney 	 * f_pos field of file structure
86098297b2SJeff Mahoney 	 */
874acf381eSAl Viro 	make_cpu_key(&pos_key, inode, ctx->pos ?: DOT_OFFSET, TYPE_DIRENTRY, 3);
881da177e4SLinus Torvalds 	next_pos = cpu_key_k_offset(&pos_key);
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds 	path_to_entry.reada = PATH_READA;
911da177e4SLinus Torvalds 	while (1) {
921da177e4SLinus Torvalds research:
93098297b2SJeff Mahoney 		/*
94098297b2SJeff Mahoney 		 * search the directory item, containing entry with
95098297b2SJeff Mahoney 		 * specified key
96098297b2SJeff Mahoney 		 */
97bd4c625cSLinus Torvalds 		search_res =
98bd4c625cSLinus Torvalds 		    search_by_entry_key(inode->i_sb, &pos_key, &path_to_entry,
99bd4c625cSLinus Torvalds 					&de);
1001da177e4SLinus Torvalds 		if (search_res == IO_ERROR) {
101098297b2SJeff Mahoney 			/*
102098297b2SJeff Mahoney 			 * FIXME: we could just skip part of directory
103098297b2SJeff Mahoney 			 * which could not be read
104098297b2SJeff Mahoney 			 */
1051da177e4SLinus Torvalds 			ret = -EIO;
1061da177e4SLinus Torvalds 			goto out;
1071da177e4SLinus Torvalds 		}
1081da177e4SLinus Torvalds 		entry_num = de.de_entry_num;
1091da177e4SLinus Torvalds 		bh = de.de_bh;
1101da177e4SLinus Torvalds 		item_num = de.de_item_num;
1111da177e4SLinus Torvalds 		ih = de.de_ih;
1121da177e4SLinus Torvalds 		store_ih(&tmp_ih, ih);
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds 		/* we must have found item, that is item of this directory, */
115a228bf8fSJeff Mahoney 		RFALSE(COMP_SHORT_KEYS(&ih->ih_key, &pos_key),
1161da177e4SLinus Torvalds 		       "vs-9000: found item %h does not match to dir we readdir %K",
1171da177e4SLinus Torvalds 		       ih, &pos_key);
1181da177e4SLinus Torvalds 		RFALSE(item_num > B_NR_ITEMS(bh) - 1,
1191da177e4SLinus Torvalds 		       "vs-9005 item_num == %d, item amount == %d",
1201da177e4SLinus Torvalds 		       item_num, B_NR_ITEMS(bh));
1211da177e4SLinus Torvalds 
122098297b2SJeff Mahoney 		/*
123098297b2SJeff Mahoney 		 * and entry must be not more than number of entries
124098297b2SJeff Mahoney 		 * in the item
125098297b2SJeff Mahoney 		 */
1264cf5f7adSJeff Mahoney 		RFALSE(ih_entry_count(ih) < entry_num,
1271da177e4SLinus Torvalds 		       "vs-9010: entry number is too big %d (%d)",
1284cf5f7adSJeff Mahoney 		       entry_num, ih_entry_count(ih));
1291da177e4SLinus Torvalds 
130098297b2SJeff Mahoney 		/*
131098297b2SJeff Mahoney 		 * go through all entries in the directory item beginning
132098297b2SJeff Mahoney 		 * from the entry, that has been found
133098297b2SJeff Mahoney 		 */
134bd4c625cSLinus Torvalds 		if (search_res == POSITION_FOUND
1354cf5f7adSJeff Mahoney 		    || entry_num < ih_entry_count(ih)) {
136bd4c625cSLinus Torvalds 			struct reiserfs_de_head *deh =
137bd4c625cSLinus Torvalds 			    B_I_DEH(bh, ih) + entry_num;
1381da177e4SLinus Torvalds 
1394cf5f7adSJeff Mahoney 			for (; entry_num < ih_entry_count(ih);
140bd4c625cSLinus Torvalds 			     entry_num++, deh++) {
1411da177e4SLinus Torvalds 				int d_reclen;
1421da177e4SLinus Torvalds 				char *d_name;
1431da177e4SLinus Torvalds 				ino_t d_ino;
14401d88857SJeff Mahoney 				loff_t cur_pos = deh_offset(deh);
1451da177e4SLinus Torvalds 
1461da177e4SLinus Torvalds 				/* it is hidden entry */
147098297b2SJeff Mahoney 				if (!de_visible(deh))
1481da177e4SLinus Torvalds 					continue;
1491da177e4SLinus Torvalds 				d_reclen = entry_length(bh, ih, entry_num);
1501da177e4SLinus Torvalds 				d_name = B_I_DEH_ENTRY_FILE_NAME(bh, ih, deh);
15180eb68d2SLepton Wu 
15280eb68d2SLepton Wu 				if (d_reclen <= 0 ||
15380eb68d2SLepton Wu 				    d_name + d_reclen > bh->b_data + bh->b_size) {
154098297b2SJeff Mahoney 					/*
155098297b2SJeff Mahoney 					 * There is corrupted data in entry,
156098297b2SJeff Mahoney 					 * We'd better stop here
157098297b2SJeff Mahoney 					 */
15880eb68d2SLepton Wu 					pathrelse(&path_to_entry);
15980eb68d2SLepton Wu 					ret = -EIO;
16080eb68d2SLepton Wu 					goto out;
16180eb68d2SLepton Wu 				}
16280eb68d2SLepton Wu 
1631da177e4SLinus Torvalds 				if (!d_name[d_reclen - 1])
1641da177e4SLinus Torvalds 					d_reclen = strlen(d_name);
1651da177e4SLinus Torvalds 
166098297b2SJeff Mahoney 				/* too big to send back to VFS */
167bd4c625cSLinus Torvalds 				if (d_reclen >
168bd4c625cSLinus Torvalds 				    REISERFS_MAX_NAME(inode->i_sb->
169bd4c625cSLinus Torvalds 						      s_blocksize)) {
1701da177e4SLinus Torvalds 					continue;
1711da177e4SLinus Torvalds 				}
1721da177e4SLinus Torvalds 
1731da177e4SLinus Torvalds 				/* Ignore the .reiserfs_priv entry */
17499ce4169SAl Viro 				if (is_privroot_deh(inode, deh))
1751da177e4SLinus Torvalds 					continue;
1761da177e4SLinus Torvalds 
1774acf381eSAl Viro 				ctx->pos = deh_offset(deh);
1781da177e4SLinus Torvalds 				d_ino = deh_objectid(deh);
1791da177e4SLinus Torvalds 				if (d_reclen <= 32) {
1801da177e4SLinus Torvalds 					local_buf = small_buf;
1811da177e4SLinus Torvalds 				} else {
182d739b42bSPekka Enberg 					local_buf = kmalloc(d_reclen,
183d739b42bSPekka Enberg 							    GFP_NOFS);
1841da177e4SLinus Torvalds 					if (!local_buf) {
1851da177e4SLinus Torvalds 						pathrelse(&path_to_entry);
1861da177e4SLinus Torvalds 						ret = -ENOMEM;
1871da177e4SLinus Torvalds 						goto out;
1881da177e4SLinus Torvalds 					}
1891da177e4SLinus Torvalds 					if (item_moved(&tmp_ih, &path_to_entry)) {
190d739b42bSPekka Enberg 						kfree(local_buf);
1911da177e4SLinus Torvalds 						goto research;
1921da177e4SLinus Torvalds 					}
1931da177e4SLinus Torvalds 				}
194098297b2SJeff Mahoney 
195098297b2SJeff Mahoney 				/*
196098297b2SJeff Mahoney 				 * Note, that we copy name to user space via
197098297b2SJeff Mahoney 				 * temporary buffer (local_buf) because
198098297b2SJeff Mahoney 				 * filldir will block if user space buffer is
199098297b2SJeff Mahoney 				 * swapped out. At that time entry can move to
200098297b2SJeff Mahoney 				 * somewhere else
201098297b2SJeff Mahoney 				 */
2021da177e4SLinus Torvalds 				memcpy(local_buf, d_name, d_reclen);
2038ebc4232SFrederic Weisbecker 
2048ebc4232SFrederic Weisbecker 				/*
2058ebc4232SFrederic Weisbecker 				 * Since filldir might sleep, we can release
2068ebc4232SFrederic Weisbecker 				 * the write lock here for other waiters
2078ebc4232SFrederic Weisbecker 				 */
208278f6679SJeff Mahoney 				depth = reiserfs_write_unlock_nested(inode->i_sb);
2094acf381eSAl Viro 				if (!dir_emit
2104acf381eSAl Viro 				    (ctx, local_buf, d_reclen, d_ino,
2114acf381eSAl Viro 				     DT_UNKNOWN)) {
212278f6679SJeff Mahoney 					reiserfs_write_lock_nested(inode->i_sb, depth);
2131da177e4SLinus Torvalds 					if (local_buf != small_buf) {
214d739b42bSPekka Enberg 						kfree(local_buf);
2151da177e4SLinus Torvalds 					}
2161da177e4SLinus Torvalds 					goto end;
2171da177e4SLinus Torvalds 				}
218278f6679SJeff Mahoney 				reiserfs_write_lock_nested(inode->i_sb, depth);
2191da177e4SLinus Torvalds 				if (local_buf != small_buf) {
220d739b42bSPekka Enberg 					kfree(local_buf);
2211da177e4SLinus Torvalds 				}
22201d88857SJeff Mahoney 
22301d88857SJeff Mahoney 				/* deh_offset(deh) may be invalid now. */
22401d88857SJeff Mahoney 				next_pos = cur_pos + 1;
2251da177e4SLinus Torvalds 
2261da177e4SLinus Torvalds 				if (item_moved(&tmp_ih, &path_to_entry)) {
2270bdc7acbSJeff Mahoney 					set_cpu_key_k_offset(&pos_key,
2280bdc7acbSJeff Mahoney 							     next_pos);
2291da177e4SLinus Torvalds 					goto research;
2301da177e4SLinus Torvalds 				}
2311da177e4SLinus Torvalds 			}	/* for */
2321da177e4SLinus Torvalds 		}
2331da177e4SLinus Torvalds 
234098297b2SJeff Mahoney 		/* end of directory has been reached */
2351da177e4SLinus Torvalds 		if (item_num != B_NR_ITEMS(bh) - 1)
2361da177e4SLinus Torvalds 			goto end;
2371da177e4SLinus Torvalds 
238098297b2SJeff Mahoney 		/*
239098297b2SJeff Mahoney 		 * item we went through is last item of node. Using right
240098297b2SJeff Mahoney 		 * delimiting key check is it directory end
241098297b2SJeff Mahoney 		 */
2421da177e4SLinus Torvalds 		rkey = get_rkey(&path_to_entry, inode->i_sb);
2431da177e4SLinus Torvalds 		if (!comp_le_keys(rkey, &MIN_KEY)) {
244098297b2SJeff Mahoney 			/*
245098297b2SJeff Mahoney 			 * set pos_key to key, that is the smallest and greater
246098297b2SJeff Mahoney 			 * that key of the last entry in the item
247098297b2SJeff Mahoney 			 */
2481da177e4SLinus Torvalds 			set_cpu_key_k_offset(&pos_key, next_pos);
2491da177e4SLinus Torvalds 			continue;
2501da177e4SLinus Torvalds 		}
2511da177e4SLinus Torvalds 
252098297b2SJeff Mahoney 		/* end of directory has been reached */
2531da177e4SLinus Torvalds 		if (COMP_SHORT_KEYS(rkey, &pos_key)) {
2541da177e4SLinus Torvalds 			goto end;
2551da177e4SLinus Torvalds 		}
2561da177e4SLinus Torvalds 
2571da177e4SLinus Torvalds 		/* directory continues in the right neighboring block */
258bd4c625cSLinus Torvalds 		set_cpu_key_k_offset(&pos_key,
259bd4c625cSLinus Torvalds 				     le_key_k_offset(KEY_FORMAT_3_5, rkey));
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds 	}			/* while */
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds end:
2644acf381eSAl Viro 	ctx->pos = next_pos;
2651da177e4SLinus Torvalds 	pathrelse(&path_to_entry);
2661da177e4SLinus Torvalds 	reiserfs_check_path(&path_to_entry);
2671da177e4SLinus Torvalds out:
2681da177e4SLinus Torvalds 	reiserfs_write_unlock(inode->i_sb);
2691da177e4SLinus Torvalds 	return ret;
2701da177e4SLinus Torvalds }
2711da177e4SLinus Torvalds 
reiserfs_readdir(struct file * file,struct dir_context * ctx)2724acf381eSAl Viro static int reiserfs_readdir(struct file *file, struct dir_context *ctx)
273a41f1a47SJeff Mahoney {
274cd62cdaeSAl Viro 	return reiserfs_readdir_inode(file_inode(file), ctx);
275a41f1a47SJeff Mahoney }
276a41f1a47SJeff Mahoney 
277098297b2SJeff Mahoney /*
278098297b2SJeff Mahoney  * compose directory item containing "." and ".." entries (entries are
279098297b2SJeff Mahoney  * not aligned to 4 byte boundary)
280098297b2SJeff Mahoney  */
make_empty_dir_item_v1(char * body,__le32 dirid,__le32 objid,__le32 par_dirid,__le32 par_objid)2813e8962beSAl Viro void make_empty_dir_item_v1(char *body, __le32 dirid, __le32 objid,
2823e8962beSAl Viro 			    __le32 par_dirid, __le32 par_objid)
2831da177e4SLinus Torvalds {
28416da167cSJeff Mahoney 	struct reiserfs_de_head *dot, *dotdot;
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds 	memset(body, 0, EMPTY_DIR_SIZE_V1);
28716da167cSJeff Mahoney 	dot = (struct reiserfs_de_head *)body;
28816da167cSJeff Mahoney 	dotdot = dot + 1;
2891da177e4SLinus Torvalds 
2901da177e4SLinus Torvalds 	/* direntry header of "." */
29116da167cSJeff Mahoney 	put_deh_offset(dot, DOT_OFFSET);
292*9436fb4dSRandy Dunlap 	/* these two are from make_le_item_head, and are LE */
29316da167cSJeff Mahoney 	dot->deh_dir_id = dirid;
29416da167cSJeff Mahoney 	dot->deh_objectid = objid;
29516da167cSJeff Mahoney 	dot->deh_state = 0;	/* Endian safe if 0 */
29616da167cSJeff Mahoney 	put_deh_location(dot, EMPTY_DIR_SIZE_V1 - strlen("."));
29716da167cSJeff Mahoney 	mark_de_visible(dot);
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds 	/* direntry header of ".." */
30016da167cSJeff Mahoney 	put_deh_offset(dotdot, DOT_DOT_OFFSET);
3011da177e4SLinus Torvalds 	/* key of ".." for the root directory */
302*9436fb4dSRandy Dunlap 	/* these two are from the inode, and are LE */
30316da167cSJeff Mahoney 	dotdot->deh_dir_id = par_dirid;
30416da167cSJeff Mahoney 	dotdot->deh_objectid = par_objid;
30516da167cSJeff Mahoney 	dotdot->deh_state = 0;	/* Endian safe if 0 */
30616da167cSJeff Mahoney 	put_deh_location(dotdot, deh_location(dot) - strlen(".."));
30716da167cSJeff Mahoney 	mark_de_visible(dotdot);
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds 	/* copy ".." and "." */
31016da167cSJeff Mahoney 	memcpy(body + deh_location(dot), ".", 1);
31116da167cSJeff Mahoney 	memcpy(body + deh_location(dotdot), "..", 2);
3121da177e4SLinus Torvalds }
3131da177e4SLinus Torvalds 
3141da177e4SLinus Torvalds /* compose directory item containing "." and ".." entries */
make_empty_dir_item(char * body,__le32 dirid,__le32 objid,__le32 par_dirid,__le32 par_objid)3153e8962beSAl Viro void make_empty_dir_item(char *body, __le32 dirid, __le32 objid,
3163e8962beSAl Viro 			 __le32 par_dirid, __le32 par_objid)
3171da177e4SLinus Torvalds {
31816da167cSJeff Mahoney 	struct reiserfs_de_head *dot, *dotdot;
3191da177e4SLinus Torvalds 
3201da177e4SLinus Torvalds 	memset(body, 0, EMPTY_DIR_SIZE);
32116da167cSJeff Mahoney 	dot = (struct reiserfs_de_head *)body;
32216da167cSJeff Mahoney 	dotdot = dot + 1;
3231da177e4SLinus Torvalds 
3241da177e4SLinus Torvalds 	/* direntry header of "." */
32516da167cSJeff Mahoney 	put_deh_offset(dot, DOT_OFFSET);
326*9436fb4dSRandy Dunlap 	/* these two are from make_le_item_head, and are LE */
32716da167cSJeff Mahoney 	dot->deh_dir_id = dirid;
32816da167cSJeff Mahoney 	dot->deh_objectid = objid;
32916da167cSJeff Mahoney 	dot->deh_state = 0;	/* Endian safe if 0 */
33016da167cSJeff Mahoney 	put_deh_location(dot, EMPTY_DIR_SIZE - ROUND_UP(strlen(".")));
33116da167cSJeff Mahoney 	mark_de_visible(dot);
3321da177e4SLinus Torvalds 
3331da177e4SLinus Torvalds 	/* direntry header of ".." */
33416da167cSJeff Mahoney 	put_deh_offset(dotdot, DOT_DOT_OFFSET);
3351da177e4SLinus Torvalds 	/* key of ".." for the root directory */
336*9436fb4dSRandy Dunlap 	/* these two are from the inode, and are LE */
33716da167cSJeff Mahoney 	dotdot->deh_dir_id = par_dirid;
33816da167cSJeff Mahoney 	dotdot->deh_objectid = par_objid;
33916da167cSJeff Mahoney 	dotdot->deh_state = 0;	/* Endian safe if 0 */
34016da167cSJeff Mahoney 	put_deh_location(dotdot, deh_location(dot) - ROUND_UP(strlen("..")));
34116da167cSJeff Mahoney 	mark_de_visible(dotdot);
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds 	/* copy ".." and "." */
34416da167cSJeff Mahoney 	memcpy(body + deh_location(dot), ".", 1);
34516da167cSJeff Mahoney 	memcpy(body + deh_location(dotdot), "..", 2);
3461da177e4SLinus Torvalds }
347