| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1 | /* -*- mode: c; c-basic-offset: 8; -*- | 
 | 2 |  * vim: noexpandtab sw=8 ts=8 sts=0: | 
 | 3 |  * | 
 | 4 |  * dir.c | 
 | 5 |  * | 
 | 6 |  * Creates, reads, walks and deletes directory-nodes | 
 | 7 |  * | 
 | 8 |  * Copyright (C) 2002, 2004 Oracle.  All rights reserved. | 
 | 9 |  * | 
 | 10 |  *  Portions of this code from linux/fs/ext3/dir.c | 
 | 11 |  * | 
 | 12 |  *  Copyright (C) 1992, 1993, 1994, 1995 | 
 | 13 |  *  Remy Card (card@masi.ibp.fr) | 
 | 14 |  *  Laboratoire MASI - Institut Blaise pascal | 
 | 15 |  *  Universite Pierre et Marie Curie (Paris VI) | 
 | 16 |  * | 
 | 17 |  *   from | 
 | 18 |  * | 
 | 19 |  *   linux/fs/minix/dir.c | 
 | 20 |  * | 
 | 21 |  *   Copyright (C) 1991, 1992 Linux Torvalds | 
 | 22 |  * | 
 | 23 |  * This program is free software; you can redistribute it and/or | 
 | 24 |  * modify it under the terms of the GNU General Public | 
 | 25 |  * License as published by the Free Software Foundation; either | 
 | 26 |  * version 2 of the License, or (at your option) any later version. | 
 | 27 |  * | 
 | 28 |  * This program is distributed in the hope that it will be useful, | 
 | 29 |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
 | 30 |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | 
 | 31 |  * General Public License for more details. | 
 | 32 |  * | 
 | 33 |  * You should have received a copy of the GNU General Public | 
 | 34 |  * License along with this program; if not, write to the | 
 | 35 |  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, | 
 | 36 |  * Boston, MA 021110-1307, USA. | 
 | 37 |  */ | 
 | 38 |  | 
 | 39 | #include <linux/fs.h> | 
 | 40 | #include <linux/types.h> | 
 | 41 | #include <linux/slab.h> | 
 | 42 | #include <linux/highmem.h> | 
| Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 43 | #include <linux/quotaops.h> | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 44 |  | 
 | 45 | #define MLOG_MASK_PREFIX ML_NAMEI | 
 | 46 | #include <cluster/masklog.h> | 
 | 47 |  | 
 | 48 | #include "ocfs2.h" | 
 | 49 |  | 
 | 50 | #include "alloc.h" | 
| Joel Becker | c175a51 | 2008-12-10 17:58:22 -0800 | [diff] [blame] | 51 | #include "blockcheck.h" | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 52 | #include "dir.h" | 
 | 53 | #include "dlmglue.h" | 
 | 54 | #include "extent_map.h" | 
 | 55 | #include "file.h" | 
 | 56 | #include "inode.h" | 
 | 57 | #include "journal.h" | 
 | 58 | #include "namei.h" | 
 | 59 | #include "suballoc.h" | 
| Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 60 | #include "super.h" | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 61 | #include "uptodate.h" | 
 | 62 |  | 
 | 63 | #include "buffer_head_io.h" | 
 | 64 |  | 
| Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 65 | #define NAMEI_RA_CHUNKS  2 | 
 | 66 | #define NAMEI_RA_BLOCKS  4 | 
 | 67 | #define NAMEI_RA_SIZE        (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS) | 
 | 68 | #define NAMEI_RA_INDEX(c,b)  (((c) * NAMEI_RA_BLOCKS) + (b)) | 
 | 69 |  | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 70 | static unsigned char ocfs2_filetype_table[] = { | 
 | 71 | 	DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK | 
 | 72 | }; | 
 | 73 |  | 
 | 74 | static int ocfs2_extend_dir(struct ocfs2_super *osb, | 
 | 75 | 			    struct inode *dir, | 
 | 76 | 			    struct buffer_head *parent_fe_bh, | 
| Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 77 | 			    unsigned int blocks_wanted, | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 78 | 			    struct buffer_head **new_de_bh); | 
| Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 79 | static int ocfs2_do_extend_dir(struct super_block *sb, | 
 | 80 | 			       handle_t *handle, | 
 | 81 | 			       struct inode *dir, | 
 | 82 | 			       struct buffer_head *parent_fe_bh, | 
 | 83 | 			       struct ocfs2_alloc_context *data_ac, | 
 | 84 | 			       struct ocfs2_alloc_context *meta_ac, | 
 | 85 | 			       struct buffer_head **new_bh); | 
 | 86 |  | 
| Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 87 | /* | 
| Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 88 |  * These are distinct checks because future versions of the file system will | 
 | 89 |  * want to have a trailing dirent structure independent of indexing. | 
 | 90 |  */ | 
 | 91 | static int ocfs2_dir_has_trailer(struct inode *dir) | 
 | 92 | { | 
 | 93 | 	if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) | 
 | 94 | 		return 0; | 
 | 95 |  | 
 | 96 | 	return ocfs2_meta_ecc(OCFS2_SB(dir->i_sb)); | 
 | 97 | } | 
 | 98 |  | 
 | 99 | static int ocfs2_supports_dir_trailer(struct ocfs2_super *osb) | 
 | 100 | { | 
 | 101 | 	return ocfs2_meta_ecc(osb); | 
 | 102 | } | 
 | 103 |  | 
 | 104 | static inline unsigned int ocfs2_dir_trailer_blk_off(struct super_block *sb) | 
 | 105 | { | 
 | 106 | 	return sb->s_blocksize - sizeof(struct ocfs2_dir_block_trailer); | 
 | 107 | } | 
 | 108 |  | 
 | 109 | #define ocfs2_trailer_from_bh(_bh, _sb) ((struct ocfs2_dir_block_trailer *) ((_bh)->b_data + ocfs2_dir_trailer_blk_off((_sb)))) | 
 | 110 |  | 
| Joel Becker | c175a51 | 2008-12-10 17:58:22 -0800 | [diff] [blame] | 111 | /* XXX ocfs2_block_dqtrailer() is similar but not quite - can we make | 
 | 112 |  * them more consistent? */ | 
 | 113 | struct ocfs2_dir_block_trailer *ocfs2_dir_trailer_from_size(int blocksize, | 
 | 114 | 							    void *data) | 
 | 115 | { | 
 | 116 | 	char *p = data; | 
 | 117 |  | 
 | 118 | 	p += blocksize - sizeof(struct ocfs2_dir_block_trailer); | 
 | 119 | 	return (struct ocfs2_dir_block_trailer *)p; | 
 | 120 | } | 
 | 121 |  | 
| Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 122 | /* | 
 | 123 |  * XXX: This is executed once on every dirent. We should consider optimizing | 
 | 124 |  * it. | 
 | 125 |  */ | 
 | 126 | static int ocfs2_skip_dir_trailer(struct inode *dir, | 
 | 127 | 				  struct ocfs2_dir_entry *de, | 
 | 128 | 				  unsigned long offset, | 
 | 129 | 				  unsigned long blklen) | 
 | 130 | { | 
 | 131 | 	unsigned long toff = blklen - sizeof(struct ocfs2_dir_block_trailer); | 
 | 132 |  | 
 | 133 | 	if (!ocfs2_dir_has_trailer(dir)) | 
 | 134 | 		return 0; | 
 | 135 |  | 
 | 136 | 	if (offset != toff) | 
 | 137 | 		return 0; | 
 | 138 |  | 
 | 139 | 	return 1; | 
 | 140 | } | 
 | 141 |  | 
 | 142 | static void ocfs2_init_dir_trailer(struct inode *inode, | 
 | 143 | 				   struct buffer_head *bh) | 
 | 144 | { | 
 | 145 | 	struct ocfs2_dir_block_trailer *trailer; | 
 | 146 |  | 
 | 147 | 	trailer = ocfs2_trailer_from_bh(bh, inode->i_sb); | 
 | 148 | 	strcpy(trailer->db_signature, OCFS2_DIR_TRAILER_SIGNATURE); | 
 | 149 | 	trailer->db_compat_rec_len = | 
 | 150 | 			cpu_to_le16(sizeof(struct ocfs2_dir_block_trailer)); | 
 | 151 | 	trailer->db_parent_dinode = cpu_to_le64(OCFS2_I(inode)->ip_blkno); | 
 | 152 | 	trailer->db_blkno = cpu_to_le64(bh->b_blocknr); | 
 | 153 | } | 
 | 154 |  | 
 | 155 | /* | 
| Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 156 |  * bh passed here can be an inode block or a dir data block, depending | 
 | 157 |  * on the inode inline data flag. | 
 | 158 |  */ | 
| Mark Fasheh | 5eae5b9 | 2007-09-10 17:50:51 -0700 | [diff] [blame] | 159 | static int ocfs2_check_dir_entry(struct inode * dir, | 
 | 160 | 				 struct ocfs2_dir_entry * de, | 
 | 161 | 				 struct buffer_head * bh, | 
 | 162 | 				 unsigned long offset) | 
| Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 163 | { | 
 | 164 | 	const char *error_msg = NULL; | 
 | 165 | 	const int rlen = le16_to_cpu(de->rec_len); | 
 | 166 |  | 
 | 167 | 	if (rlen < OCFS2_DIR_REC_LEN(1)) | 
 | 168 | 		error_msg = "rec_len is smaller than minimal"; | 
 | 169 | 	else if (rlen % 4 != 0) | 
 | 170 | 		error_msg = "rec_len % 4 != 0"; | 
 | 171 | 	else if (rlen < OCFS2_DIR_REC_LEN(de->name_len)) | 
 | 172 | 		error_msg = "rec_len is too small for name_len"; | 
 | 173 | 	else if (((char *) de - bh->b_data) + rlen > dir->i_sb->s_blocksize) | 
 | 174 | 		error_msg = "directory entry across blocks"; | 
 | 175 |  | 
 | 176 | 	if (error_msg != NULL) | 
 | 177 | 		mlog(ML_ERROR, "bad entry in directory #%llu: %s - " | 
 | 178 | 		     "offset=%lu, inode=%llu, rec_len=%d, name_len=%d\n", | 
 | 179 | 		     (unsigned long long)OCFS2_I(dir)->ip_blkno, error_msg, | 
 | 180 | 		     offset, (unsigned long long)le64_to_cpu(de->inode), rlen, | 
 | 181 | 		     de->name_len); | 
 | 182 | 	return error_msg == NULL ? 1 : 0; | 
 | 183 | } | 
 | 184 |  | 
 | 185 | static inline int ocfs2_match(int len, | 
 | 186 | 			      const char * const name, | 
 | 187 | 			      struct ocfs2_dir_entry *de) | 
 | 188 | { | 
 | 189 | 	if (len != de->name_len) | 
 | 190 | 		return 0; | 
 | 191 | 	if (!de->inode) | 
 | 192 | 		return 0; | 
 | 193 | 	return !memcmp(name, de->name, len); | 
 | 194 | } | 
 | 195 |  | 
 | 196 | /* | 
 | 197 |  * Returns 0 if not found, -1 on failure, and 1 on success | 
 | 198 |  */ | 
 | 199 | static int inline ocfs2_search_dirblock(struct buffer_head *bh, | 
 | 200 | 					struct inode *dir, | 
 | 201 | 					const char *name, int namelen, | 
 | 202 | 					unsigned long offset, | 
| Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 203 | 					char *first_de, | 
 | 204 | 					unsigned int bytes, | 
| Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 205 | 					struct ocfs2_dir_entry **res_dir) | 
 | 206 | { | 
 | 207 | 	struct ocfs2_dir_entry *de; | 
 | 208 | 	char *dlimit, *de_buf; | 
 | 209 | 	int de_len; | 
 | 210 | 	int ret = 0; | 
 | 211 |  | 
 | 212 | 	mlog_entry_void(); | 
 | 213 |  | 
| Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 214 | 	de_buf = first_de; | 
 | 215 | 	dlimit = de_buf + bytes; | 
| Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 216 |  | 
 | 217 | 	while (de_buf < dlimit) { | 
 | 218 | 		/* this code is executed quadratically often */ | 
 | 219 | 		/* do minimal checking `by hand' */ | 
 | 220 |  | 
 | 221 | 		de = (struct ocfs2_dir_entry *) de_buf; | 
 | 222 |  | 
 | 223 | 		if (de_buf + namelen <= dlimit && | 
 | 224 | 		    ocfs2_match(namelen, name, de)) { | 
 | 225 | 			/* found a match - just to be sure, do a full check */ | 
 | 226 | 			if (!ocfs2_check_dir_entry(dir, de, bh, offset)) { | 
 | 227 | 				ret = -1; | 
 | 228 | 				goto bail; | 
 | 229 | 			} | 
 | 230 | 			*res_dir = de; | 
 | 231 | 			ret = 1; | 
 | 232 | 			goto bail; | 
 | 233 | 		} | 
 | 234 |  | 
 | 235 | 		/* prevent looping on a bad block */ | 
 | 236 | 		de_len = le16_to_cpu(de->rec_len); | 
 | 237 | 		if (de_len <= 0) { | 
 | 238 | 			ret = -1; | 
 | 239 | 			goto bail; | 
 | 240 | 		} | 
 | 241 |  | 
 | 242 | 		de_buf += de_len; | 
 | 243 | 		offset += de_len; | 
 | 244 | 	} | 
 | 245 |  | 
 | 246 | bail: | 
 | 247 | 	mlog_exit(ret); | 
 | 248 | 	return ret; | 
 | 249 | } | 
 | 250 |  | 
| Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 251 | static struct buffer_head *ocfs2_find_entry_id(const char *name, | 
 | 252 | 					       int namelen, | 
 | 253 | 					       struct inode *dir, | 
 | 254 | 					       struct ocfs2_dir_entry **res_dir) | 
 | 255 | { | 
 | 256 | 	int ret, found; | 
 | 257 | 	struct buffer_head *di_bh = NULL; | 
 | 258 | 	struct ocfs2_dinode *di; | 
 | 259 | 	struct ocfs2_inline_data *data; | 
 | 260 |  | 
| Joel Becker | b657c95 | 2008-11-13 14:49:11 -0800 | [diff] [blame] | 261 | 	ret = ocfs2_read_inode_block(dir, &di_bh); | 
| Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 262 | 	if (ret) { | 
 | 263 | 		mlog_errno(ret); | 
 | 264 | 		goto out; | 
 | 265 | 	} | 
 | 266 |  | 
 | 267 | 	di = (struct ocfs2_dinode *)di_bh->b_data; | 
 | 268 | 	data = &di->id2.i_data; | 
 | 269 |  | 
 | 270 | 	found = ocfs2_search_dirblock(di_bh, dir, name, namelen, 0, | 
 | 271 | 				      data->id_data, i_size_read(dir), res_dir); | 
 | 272 | 	if (found == 1) | 
 | 273 | 		return di_bh; | 
 | 274 |  | 
 | 275 | 	brelse(di_bh); | 
 | 276 | out: | 
 | 277 | 	return NULL; | 
 | 278 | } | 
 | 279 |  | 
| Joel Becker | a22305c | 2008-11-13 14:49:17 -0800 | [diff] [blame] | 280 | static int ocfs2_validate_dir_block(struct super_block *sb, | 
 | 281 | 				    struct buffer_head *bh) | 
 | 282 | { | 
| Joel Becker | c175a51 | 2008-12-10 17:58:22 -0800 | [diff] [blame] | 283 | 	int rc; | 
 | 284 | 	struct ocfs2_dir_block_trailer *trailer = | 
 | 285 | 		ocfs2_trailer_from_bh(bh, sb); | 
 | 286 |  | 
 | 287 |  | 
| Joel Becker | a22305c | 2008-11-13 14:49:17 -0800 | [diff] [blame] | 288 | 	/* | 
| Joel Becker | c175a51 | 2008-12-10 17:58:22 -0800 | [diff] [blame] | 289 | 	 * We don't validate dirents here, that's handled | 
| Joel Becker | a22305c | 2008-11-13 14:49:17 -0800 | [diff] [blame] | 290 | 	 * in-place when the code walks them. | 
 | 291 | 	 */ | 
| Joel Becker | 970e493 | 2008-11-13 14:49:19 -0800 | [diff] [blame] | 292 | 	mlog(0, "Validating dirblock %llu\n", | 
 | 293 | 	     (unsigned long long)bh->b_blocknr); | 
| Joel Becker | a22305c | 2008-11-13 14:49:17 -0800 | [diff] [blame] | 294 |  | 
| Joel Becker | c175a51 | 2008-12-10 17:58:22 -0800 | [diff] [blame] | 295 | 	BUG_ON(!buffer_uptodate(bh)); | 
 | 296 |  | 
 | 297 | 	/* | 
 | 298 | 	 * If the ecc fails, we return the error but otherwise | 
 | 299 | 	 * leave the filesystem running.  We know any error is | 
 | 300 | 	 * local to this block. | 
 | 301 | 	 * | 
 | 302 | 	 * Note that we are safe to call this even if the directory | 
 | 303 | 	 * doesn't have a trailer.  Filesystems without metaecc will do | 
 | 304 | 	 * nothing, and filesystems with it will have one. | 
 | 305 | 	 */ | 
 | 306 | 	rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &trailer->db_check); | 
 | 307 | 	if (rc) | 
 | 308 | 		mlog(ML_ERROR, "Checksum failed for dinode %llu\n", | 
 | 309 | 		     (unsigned long long)bh->b_blocknr); | 
 | 310 |  | 
 | 311 | 	return rc; | 
| Joel Becker | a22305c | 2008-11-13 14:49:17 -0800 | [diff] [blame] | 312 | } | 
 | 313 |  | 
 | 314 | /* | 
 | 315 |  * This function forces all errors to -EIO for consistency with its | 
 | 316 |  * predecessor, ocfs2_bread().  We haven't audited what returning the | 
 | 317 |  * real error codes would do to callers.  We log the real codes with | 
 | 318 |  * mlog_errno() before we squash them. | 
 | 319 |  */ | 
 | 320 | static int ocfs2_read_dir_block(struct inode *inode, u64 v_block, | 
 | 321 | 				struct buffer_head **bh, int flags) | 
 | 322 | { | 
 | 323 | 	int rc = 0; | 
 | 324 | 	struct buffer_head *tmp = *bh; | 
| Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 325 | 	struct ocfs2_dir_block_trailer *trailer; | 
| Joel Becker | a22305c | 2008-11-13 14:49:17 -0800 | [diff] [blame] | 326 |  | 
| Joel Becker | 511308d | 2008-11-13 14:49:21 -0800 | [diff] [blame] | 327 | 	rc = ocfs2_read_virt_blocks(inode, v_block, 1, &tmp, flags, | 
 | 328 | 				    ocfs2_validate_dir_block); | 
| Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 329 | 	if (rc) { | 
| Joel Becker | a22305c | 2008-11-13 14:49:17 -0800 | [diff] [blame] | 330 | 		mlog_errno(rc); | 
| Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 331 | 		goto out; | 
 | 332 | 	} | 
 | 333 |  | 
 | 334 | 	/* | 
 | 335 | 	 * We check the trailer here rather than in | 
 | 336 | 	 * ocfs2_validate_dir_block() because that function doesn't have | 
 | 337 | 	 * the inode to test. | 
 | 338 | 	 */ | 
 | 339 | 	if (!(flags & OCFS2_BH_READAHEAD) && | 
 | 340 | 	    ocfs2_dir_has_trailer(inode)) { | 
 | 341 | 		trailer = ocfs2_trailer_from_bh(tmp, inode->i_sb); | 
 | 342 | 		if (!OCFS2_IS_VALID_DIR_TRAILER(trailer)) { | 
 | 343 | 			rc = -EINVAL; | 
 | 344 | 			ocfs2_error(inode->i_sb, | 
 | 345 | 				    "Invalid dirblock #%llu: " | 
 | 346 | 				    "signature = %.*s\n", | 
 | 347 | 				    (unsigned long long)tmp->b_blocknr, 7, | 
 | 348 | 				    trailer->db_signature); | 
 | 349 | 			goto out; | 
 | 350 | 		} | 
 | 351 | 		if (le64_to_cpu(trailer->db_blkno) != tmp->b_blocknr) { | 
 | 352 | 			rc = -EINVAL; | 
 | 353 | 			ocfs2_error(inode->i_sb, | 
 | 354 | 				    "Directory block #%llu has an invalid " | 
 | 355 | 				    "db_blkno of %llu", | 
 | 356 | 				    (unsigned long long)tmp->b_blocknr, | 
 | 357 | 				    (unsigned long long)le64_to_cpu(trailer->db_blkno)); | 
 | 358 | 			goto out; | 
 | 359 | 		} | 
 | 360 | 		if (le64_to_cpu(trailer->db_parent_dinode) != | 
 | 361 | 		    OCFS2_I(inode)->ip_blkno) { | 
 | 362 | 			rc = -EINVAL; | 
 | 363 | 			ocfs2_error(inode->i_sb, | 
 | 364 | 				    "Directory block #%llu on dinode " | 
 | 365 | 				    "#%llu has an invalid parent_dinode " | 
 | 366 | 				    "of %llu", | 
 | 367 | 				    (unsigned long long)tmp->b_blocknr, | 
 | 368 | 				    (unsigned long long)OCFS2_I(inode)->ip_blkno, | 
 | 369 | 				    (unsigned long long)le64_to_cpu(trailer->db_blkno)); | 
 | 370 | 			goto out; | 
 | 371 | 		} | 
 | 372 | 	} | 
| Joel Becker | a22305c | 2008-11-13 14:49:17 -0800 | [diff] [blame] | 373 |  | 
| Joel Becker | 511308d | 2008-11-13 14:49:21 -0800 | [diff] [blame] | 374 | 	/* If ocfs2_read_virt_blocks() got us a new bh, pass it up. */ | 
| Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 375 | 	if (!*bh) | 
| Joel Becker | a22305c | 2008-11-13 14:49:17 -0800 | [diff] [blame] | 376 | 		*bh = tmp; | 
 | 377 |  | 
| Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 378 | out: | 
| Joel Becker | a22305c | 2008-11-13 14:49:17 -0800 | [diff] [blame] | 379 | 	return rc ? -EIO : 0; | 
 | 380 | } | 
 | 381 |  | 
| Adrian Bunk | 0af4bd3 | 2007-10-24 18:23:27 +0200 | [diff] [blame] | 382 | static struct buffer_head *ocfs2_find_entry_el(const char *name, int namelen, | 
 | 383 | 					       struct inode *dir, | 
 | 384 | 					       struct ocfs2_dir_entry **res_dir) | 
| Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 385 | { | 
 | 386 | 	struct super_block *sb; | 
 | 387 | 	struct buffer_head *bh_use[NAMEI_RA_SIZE]; | 
 | 388 | 	struct buffer_head *bh, *ret = NULL; | 
 | 389 | 	unsigned long start, block, b; | 
 | 390 | 	int ra_max = 0;		/* Number of bh's in the readahead | 
 | 391 | 				   buffer, bh_use[] */ | 
 | 392 | 	int ra_ptr = 0;		/* Current index into readahead | 
 | 393 | 				   buffer */ | 
 | 394 | 	int num = 0; | 
 | 395 | 	int nblocks, i, err; | 
 | 396 |  | 
 | 397 | 	mlog_entry_void(); | 
 | 398 |  | 
| Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 399 | 	sb = dir->i_sb; | 
 | 400 |  | 
 | 401 | 	nblocks = i_size_read(dir) >> sb->s_blocksize_bits; | 
 | 402 | 	start = OCFS2_I(dir)->ip_dir_start_lookup; | 
 | 403 | 	if (start >= nblocks) | 
 | 404 | 		start = 0; | 
 | 405 | 	block = start; | 
 | 406 |  | 
 | 407 | restart: | 
 | 408 | 	do { | 
 | 409 | 		/* | 
 | 410 | 		 * We deal with the read-ahead logic here. | 
 | 411 | 		 */ | 
 | 412 | 		if (ra_ptr >= ra_max) { | 
 | 413 | 			/* Refill the readahead buffer */ | 
 | 414 | 			ra_ptr = 0; | 
 | 415 | 			b = block; | 
 | 416 | 			for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) { | 
 | 417 | 				/* | 
 | 418 | 				 * Terminate if we reach the end of the | 
 | 419 | 				 * directory and must wrap, or if our | 
 | 420 | 				 * search has finished at this block. | 
 | 421 | 				 */ | 
 | 422 | 				if (b >= nblocks || (num && block == start)) { | 
 | 423 | 					bh_use[ra_max] = NULL; | 
 | 424 | 					break; | 
 | 425 | 				} | 
 | 426 | 				num++; | 
 | 427 |  | 
| Joel Becker | a22305c | 2008-11-13 14:49:17 -0800 | [diff] [blame] | 428 | 				bh = NULL; | 
 | 429 | 				err = ocfs2_read_dir_block(dir, b++, &bh, | 
 | 430 | 							   OCFS2_BH_READAHEAD); | 
| Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 431 | 				bh_use[ra_max] = bh; | 
 | 432 | 			} | 
 | 433 | 		} | 
 | 434 | 		if ((bh = bh_use[ra_ptr++]) == NULL) | 
 | 435 | 			goto next; | 
| Joel Becker | a22305c | 2008-11-13 14:49:17 -0800 | [diff] [blame] | 436 | 		if (ocfs2_read_dir_block(dir, block, &bh, 0)) { | 
| Joel Becker | 5e0b3de | 2008-10-09 17:20:33 -0700 | [diff] [blame] | 437 | 			/* read error, skip block & hope for the best. | 
| Joel Becker | a22305c | 2008-11-13 14:49:17 -0800 | [diff] [blame] | 438 | 			 * ocfs2_read_dir_block() has released the bh. */ | 
| Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 439 | 			ocfs2_error(dir->i_sb, "reading directory %llu, " | 
 | 440 | 				    "offset %lu\n", | 
 | 441 | 				    (unsigned long long)OCFS2_I(dir)->ip_blkno, | 
 | 442 | 				    block); | 
| Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 443 | 			goto next; | 
 | 444 | 		} | 
 | 445 | 		i = ocfs2_search_dirblock(bh, dir, name, namelen, | 
 | 446 | 					  block << sb->s_blocksize_bits, | 
| Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 447 | 					  bh->b_data, sb->s_blocksize, | 
| Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 448 | 					  res_dir); | 
 | 449 | 		if (i == 1) { | 
 | 450 | 			OCFS2_I(dir)->ip_dir_start_lookup = block; | 
 | 451 | 			ret = bh; | 
 | 452 | 			goto cleanup_and_exit; | 
 | 453 | 		} else { | 
 | 454 | 			brelse(bh); | 
 | 455 | 			if (i < 0) | 
 | 456 | 				goto cleanup_and_exit; | 
 | 457 | 		} | 
 | 458 | 	next: | 
 | 459 | 		if (++block >= nblocks) | 
 | 460 | 			block = 0; | 
 | 461 | 	} while (block != start); | 
 | 462 |  | 
 | 463 | 	/* | 
 | 464 | 	 * If the directory has grown while we were searching, then | 
 | 465 | 	 * search the last part of the directory before giving up. | 
 | 466 | 	 */ | 
 | 467 | 	block = nblocks; | 
 | 468 | 	nblocks = i_size_read(dir) >> sb->s_blocksize_bits; | 
 | 469 | 	if (block < nblocks) { | 
 | 470 | 		start = 0; | 
 | 471 | 		goto restart; | 
 | 472 | 	} | 
 | 473 |  | 
 | 474 | cleanup_and_exit: | 
 | 475 | 	/* Clean up the read-ahead blocks */ | 
 | 476 | 	for (; ra_ptr < ra_max; ra_ptr++) | 
 | 477 | 		brelse(bh_use[ra_ptr]); | 
 | 478 |  | 
 | 479 | 	mlog_exit_ptr(ret); | 
 | 480 | 	return ret; | 
 | 481 | } | 
 | 482 |  | 
| Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 483 | /* | 
 | 484 |  * Try to find an entry of the provided name within 'dir'. | 
 | 485 |  * | 
 | 486 |  * If nothing was found, NULL is returned. Otherwise, a buffer_head | 
 | 487 |  * and pointer to the dir entry are passed back. | 
 | 488 |  * | 
 | 489 |  * Caller can NOT assume anything about the contents of the | 
 | 490 |  * buffer_head - it is passed back only so that it can be passed into | 
 | 491 |  * any one of the manipulation functions (add entry, delete entry, | 
 | 492 |  * etc). As an example, bh in the extent directory case is a data | 
 | 493 |  * block, in the inline-data case it actually points to an inode. | 
 | 494 |  */ | 
 | 495 | struct buffer_head *ocfs2_find_entry(const char *name, int namelen, | 
 | 496 | 				     struct inode *dir, | 
 | 497 | 				     struct ocfs2_dir_entry **res_dir) | 
 | 498 | { | 
 | 499 | 	*res_dir = NULL; | 
 | 500 |  | 
 | 501 | 	if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) | 
 | 502 | 		return ocfs2_find_entry_id(name, namelen, dir, res_dir); | 
 | 503 |  | 
 | 504 | 	return ocfs2_find_entry_el(name, namelen, dir, res_dir); | 
 | 505 | } | 
 | 506 |  | 
| Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 507 | /* | 
 | 508 |  * Update inode number and type of a previously found directory entry. | 
 | 509 |  */ | 
| Mark Fasheh | 38760e2 | 2007-09-11 17:21:56 -0700 | [diff] [blame] | 510 | int ocfs2_update_entry(struct inode *dir, handle_t *handle, | 
 | 511 | 		       struct buffer_head *de_bh, struct ocfs2_dir_entry *de, | 
 | 512 | 		       struct inode *new_entry_inode) | 
 | 513 | { | 
 | 514 | 	int ret; | 
| Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 515 | 	ocfs2_journal_access_func access = ocfs2_journal_access_db; | 
| Mark Fasheh | 38760e2 | 2007-09-11 17:21:56 -0700 | [diff] [blame] | 516 |  | 
| Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 517 | 	/* | 
 | 518 | 	 * The same code works fine for both inline-data and extent | 
| Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 519 | 	 * based directories, so no need to split this up.  The only | 
 | 520 | 	 * difference is the journal_access function. | 
| Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 521 | 	 */ | 
 | 522 |  | 
| Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 523 | 	if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) | 
 | 524 | 		access = ocfs2_journal_access_di; | 
 | 525 |  | 
 | 526 | 	ret = access(handle, dir, de_bh, OCFS2_JOURNAL_ACCESS_WRITE); | 
| Mark Fasheh | 38760e2 | 2007-09-11 17:21:56 -0700 | [diff] [blame] | 527 | 	if (ret) { | 
 | 528 | 		mlog_errno(ret); | 
 | 529 | 		goto out; | 
 | 530 | 	} | 
 | 531 |  | 
 | 532 | 	de->inode = cpu_to_le64(OCFS2_I(new_entry_inode)->ip_blkno); | 
 | 533 | 	ocfs2_set_de_type(de, new_entry_inode->i_mode); | 
 | 534 |  | 
 | 535 | 	ocfs2_journal_dirty(handle, de_bh); | 
 | 536 |  | 
 | 537 | out: | 
 | 538 | 	return ret; | 
 | 539 | } | 
 | 540 |  | 
| Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 541 | static int __ocfs2_delete_entry(handle_t *handle, struct inode *dir, | 
 | 542 | 				struct ocfs2_dir_entry *de_del, | 
 | 543 | 				struct buffer_head *bh, char *first_de, | 
 | 544 | 				unsigned int bytes) | 
| Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 545 | { | 
 | 546 | 	struct ocfs2_dir_entry *de, *pde; | 
 | 547 | 	int i, status = -ENOENT; | 
| Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 548 | 	ocfs2_journal_access_func access = ocfs2_journal_access_db; | 
| Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 549 |  | 
 | 550 | 	mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p)\n", handle, dir, de_del, bh); | 
 | 551 |  | 
| Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 552 | 	if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) | 
 | 553 | 		access = ocfs2_journal_access_di; | 
 | 554 |  | 
| Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 555 | 	i = 0; | 
 | 556 | 	pde = NULL; | 
| Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 557 | 	de = (struct ocfs2_dir_entry *) first_de; | 
 | 558 | 	while (i < bytes) { | 
| Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 559 | 		if (!ocfs2_check_dir_entry(dir, de, bh, i)) { | 
 | 560 | 			status = -EIO; | 
 | 561 | 			mlog_errno(status); | 
 | 562 | 			goto bail; | 
 | 563 | 		} | 
 | 564 | 		if (de == de_del)  { | 
| Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 565 | 			status = access(handle, dir, bh, | 
 | 566 | 					OCFS2_JOURNAL_ACCESS_WRITE); | 
| Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 567 | 			if (status < 0) { | 
 | 568 | 				status = -EIO; | 
 | 569 | 				mlog_errno(status); | 
 | 570 | 				goto bail; | 
 | 571 | 			} | 
 | 572 | 			if (pde) | 
| Marcin Slusarz | 0dd3256 | 2008-02-13 00:06:18 +0100 | [diff] [blame] | 573 | 				le16_add_cpu(&pde->rec_len, | 
 | 574 | 						le16_to_cpu(de->rec_len)); | 
| Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 575 | 			else | 
 | 576 | 				de->inode = 0; | 
 | 577 | 			dir->i_version++; | 
 | 578 | 			status = ocfs2_journal_dirty(handle, bh); | 
 | 579 | 			goto bail; | 
 | 580 | 		} | 
 | 581 | 		i += le16_to_cpu(de->rec_len); | 
 | 582 | 		pde = de; | 
 | 583 | 		de = (struct ocfs2_dir_entry *)((char *)de + le16_to_cpu(de->rec_len)); | 
 | 584 | 	} | 
 | 585 | bail: | 
 | 586 | 	mlog_exit(status); | 
 | 587 | 	return status; | 
 | 588 | } | 
 | 589 |  | 
| Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 590 | static inline int ocfs2_delete_entry_id(handle_t *handle, | 
 | 591 | 					struct inode *dir, | 
 | 592 | 					struct ocfs2_dir_entry *de_del, | 
 | 593 | 					struct buffer_head *bh) | 
 | 594 | { | 
 | 595 | 	int ret; | 
 | 596 | 	struct buffer_head *di_bh = NULL; | 
 | 597 | 	struct ocfs2_dinode *di; | 
 | 598 | 	struct ocfs2_inline_data *data; | 
 | 599 |  | 
| Joel Becker | b657c95 | 2008-11-13 14:49:11 -0800 | [diff] [blame] | 600 | 	ret = ocfs2_read_inode_block(dir, &di_bh); | 
| Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 601 | 	if (ret) { | 
 | 602 | 		mlog_errno(ret); | 
 | 603 | 		goto out; | 
 | 604 | 	} | 
 | 605 |  | 
 | 606 | 	di = (struct ocfs2_dinode *)di_bh->b_data; | 
 | 607 | 	data = &di->id2.i_data; | 
 | 608 |  | 
 | 609 | 	ret = __ocfs2_delete_entry(handle, dir, de_del, bh, data->id_data, | 
 | 610 | 				   i_size_read(dir)); | 
 | 611 |  | 
 | 612 | 	brelse(di_bh); | 
 | 613 | out: | 
 | 614 | 	return ret; | 
 | 615 | } | 
 | 616 |  | 
 | 617 | static inline int ocfs2_delete_entry_el(handle_t *handle, | 
 | 618 | 					struct inode *dir, | 
 | 619 | 					struct ocfs2_dir_entry *de_del, | 
 | 620 | 					struct buffer_head *bh) | 
 | 621 | { | 
 | 622 | 	return __ocfs2_delete_entry(handle, dir, de_del, bh, bh->b_data, | 
 | 623 | 				    bh->b_size); | 
 | 624 | } | 
 | 625 |  | 
 | 626 | /* | 
 | 627 |  * ocfs2_delete_entry deletes a directory entry by merging it with the | 
 | 628 |  * previous entry | 
 | 629 |  */ | 
 | 630 | int ocfs2_delete_entry(handle_t *handle, | 
 | 631 | 		       struct inode *dir, | 
 | 632 | 		       struct ocfs2_dir_entry *de_del, | 
 | 633 | 		       struct buffer_head *bh) | 
 | 634 | { | 
 | 635 | 	if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) | 
 | 636 | 		return ocfs2_delete_entry_id(handle, dir, de_del, bh); | 
 | 637 |  | 
 | 638 | 	return ocfs2_delete_entry_el(handle, dir, de_del, bh); | 
 | 639 | } | 
 | 640 |  | 
| Mark Fasheh | 8553cf4 | 2007-09-13 16:29:01 -0700 | [diff] [blame] | 641 | /* | 
 | 642 |  * Check whether 'de' has enough room to hold an entry of | 
 | 643 |  * 'new_rec_len' bytes. | 
 | 644 |  */ | 
 | 645 | static inline int ocfs2_dirent_would_fit(struct ocfs2_dir_entry *de, | 
 | 646 | 					 unsigned int new_rec_len) | 
 | 647 | { | 
 | 648 | 	unsigned int de_really_used; | 
 | 649 |  | 
 | 650 | 	/* Check whether this is an empty record with enough space */ | 
 | 651 | 	if (le64_to_cpu(de->inode) == 0 && | 
 | 652 | 	    le16_to_cpu(de->rec_len) >= new_rec_len) | 
 | 653 | 		return 1; | 
 | 654 |  | 
 | 655 | 	/* | 
 | 656 | 	 * Record might have free space at the end which we can | 
 | 657 | 	 * use. | 
 | 658 | 	 */ | 
 | 659 | 	de_really_used = OCFS2_DIR_REC_LEN(de->name_len); | 
 | 660 | 	if (le16_to_cpu(de->rec_len) >= (de_really_used + new_rec_len)) | 
 | 661 | 	    return 1; | 
 | 662 |  | 
 | 663 | 	return 0; | 
 | 664 | } | 
 | 665 |  | 
| Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 666 | /* we don't always have a dentry for what we want to add, so people | 
 | 667 |  * like orphan dir can call this instead. | 
 | 668 |  * | 
 | 669 |  * If you pass me insert_bh, I'll skip the search of the other dir | 
 | 670 |  * blocks and put the record in there. | 
 | 671 |  */ | 
 | 672 | int __ocfs2_add_entry(handle_t *handle, | 
 | 673 | 		      struct inode *dir, | 
 | 674 | 		      const char *name, int namelen, | 
 | 675 | 		      struct inode *inode, u64 blkno, | 
 | 676 | 		      struct buffer_head *parent_fe_bh, | 
 | 677 | 		      struct buffer_head *insert_bh) | 
 | 678 | { | 
 | 679 | 	unsigned long offset; | 
 | 680 | 	unsigned short rec_len; | 
 | 681 | 	struct ocfs2_dir_entry *de, *de1; | 
| Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 682 | 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)parent_fe_bh->b_data; | 
 | 683 | 	struct super_block *sb = dir->i_sb; | 
| Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 684 | 	int retval, status; | 
| Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 685 | 	unsigned int size = sb->s_blocksize; | 
 | 686 | 	char *data_start = insert_bh->b_data; | 
| Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 687 |  | 
 | 688 | 	mlog_entry_void(); | 
 | 689 |  | 
| Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 690 | 	if (!namelen) | 
 | 691 | 		return -EINVAL; | 
 | 692 |  | 
| Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 693 | 	if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) { | 
 | 694 | 		data_start = di->id2.i_data.id_data; | 
 | 695 | 		size = i_size_read(dir); | 
 | 696 |  | 
 | 697 | 		BUG_ON(insert_bh != parent_fe_bh); | 
 | 698 | 	} | 
 | 699 |  | 
| Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 700 | 	rec_len = OCFS2_DIR_REC_LEN(namelen); | 
 | 701 | 	offset = 0; | 
| Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 702 | 	de = (struct ocfs2_dir_entry *) data_start; | 
| Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 703 | 	while (1) { | 
| Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 704 | 		BUG_ON((char *)de >= (size + data_start)); | 
 | 705 |  | 
| Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 706 | 		/* These checks should've already been passed by the | 
 | 707 | 		 * prepare function, but I guess we can leave them | 
 | 708 | 		 * here anyway. */ | 
 | 709 | 		if (!ocfs2_check_dir_entry(dir, de, insert_bh, offset)) { | 
 | 710 | 			retval = -ENOENT; | 
 | 711 | 			goto bail; | 
 | 712 | 		} | 
 | 713 | 		if (ocfs2_match(namelen, name, de)) { | 
 | 714 | 			retval = -EEXIST; | 
 | 715 | 			goto bail; | 
 | 716 | 		} | 
| Mark Fasheh | 8553cf4 | 2007-09-13 16:29:01 -0700 | [diff] [blame] | 717 |  | 
| Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 718 | 		/* We're guaranteed that we should have space, so we | 
 | 719 | 		 * can't possibly have hit the trailer...right? */ | 
 | 720 | 		mlog_bug_on_msg(ocfs2_skip_dir_trailer(dir, de, offset, size), | 
 | 721 | 				"Hit dir trailer trying to insert %.*s " | 
 | 722 | 			        "(namelen %d) into directory %llu.  " | 
 | 723 | 				"offset is %lu, trailer offset is %d\n", | 
 | 724 | 				namelen, name, namelen, | 
 | 725 | 				(unsigned long long)parent_fe_bh->b_blocknr, | 
 | 726 | 				offset, ocfs2_dir_trailer_blk_off(dir->i_sb)); | 
 | 727 |  | 
| Mark Fasheh | 8553cf4 | 2007-09-13 16:29:01 -0700 | [diff] [blame] | 728 | 		if (ocfs2_dirent_would_fit(de, rec_len)) { | 
| Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 729 | 			dir->i_mtime = dir->i_ctime = CURRENT_TIME; | 
 | 730 | 			retval = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh); | 
 | 731 | 			if (retval < 0) { | 
 | 732 | 				mlog_errno(retval); | 
 | 733 | 				goto bail; | 
 | 734 | 			} | 
 | 735 |  | 
| Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 736 | 			if (insert_bh == parent_fe_bh) | 
 | 737 | 				status = ocfs2_journal_access_di(handle, dir, | 
 | 738 | 								 insert_bh, | 
 | 739 | 								 OCFS2_JOURNAL_ACCESS_WRITE); | 
 | 740 | 			else | 
 | 741 | 				status = ocfs2_journal_access_db(handle, dir, | 
 | 742 | 								 insert_bh, | 
 | 743 | 								 OCFS2_JOURNAL_ACCESS_WRITE); | 
| Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 744 | 			/* By now the buffer is marked for journaling */ | 
 | 745 | 			offset += le16_to_cpu(de->rec_len); | 
 | 746 | 			if (le64_to_cpu(de->inode)) { | 
 | 747 | 				de1 = (struct ocfs2_dir_entry *)((char *) de + | 
 | 748 | 					OCFS2_DIR_REC_LEN(de->name_len)); | 
 | 749 | 				de1->rec_len = | 
 | 750 | 					cpu_to_le16(le16_to_cpu(de->rec_len) - | 
 | 751 | 					OCFS2_DIR_REC_LEN(de->name_len)); | 
 | 752 | 				de->rec_len = cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len)); | 
 | 753 | 				de = de1; | 
 | 754 | 			} | 
 | 755 | 			de->file_type = OCFS2_FT_UNKNOWN; | 
 | 756 | 			if (blkno) { | 
 | 757 | 				de->inode = cpu_to_le64(blkno); | 
 | 758 | 				ocfs2_set_de_type(de, inode->i_mode); | 
 | 759 | 			} else | 
 | 760 | 				de->inode = 0; | 
 | 761 | 			de->name_len = namelen; | 
 | 762 | 			memcpy(de->name, name, namelen); | 
 | 763 |  | 
 | 764 | 			dir->i_version++; | 
 | 765 | 			status = ocfs2_journal_dirty(handle, insert_bh); | 
 | 766 | 			retval = 0; | 
 | 767 | 			goto bail; | 
 | 768 | 		} | 
| Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 769 |  | 
| Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 770 | 		offset += le16_to_cpu(de->rec_len); | 
 | 771 | 		de = (struct ocfs2_dir_entry *) ((char *) de + le16_to_cpu(de->rec_len)); | 
 | 772 | 	} | 
 | 773 |  | 
 | 774 | 	/* when you think about it, the assert above should prevent us | 
 | 775 | 	 * from ever getting here. */ | 
 | 776 | 	retval = -ENOSPC; | 
 | 777 | bail: | 
 | 778 |  | 
 | 779 | 	mlog_exit(retval); | 
 | 780 | 	return retval; | 
 | 781 | } | 
 | 782 |  | 
| Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 783 | static int ocfs2_dir_foreach_blk_id(struct inode *inode, | 
| Mathieu Desnoyers | 2b47c36 | 2007-10-16 23:27:21 -0700 | [diff] [blame] | 784 | 				    u64 *f_version, | 
| Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 785 | 				    loff_t *f_pos, void *priv, | 
| Mark Fasheh | e7b3401 | 2007-09-24 14:25:27 -0700 | [diff] [blame] | 786 | 				    filldir_t filldir, int *filldir_err) | 
| Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 787 | { | 
 | 788 | 	int ret, i, filldir_ret; | 
 | 789 | 	unsigned long offset = *f_pos; | 
 | 790 | 	struct buffer_head *di_bh = NULL; | 
 | 791 | 	struct ocfs2_dinode *di; | 
 | 792 | 	struct ocfs2_inline_data *data; | 
 | 793 | 	struct ocfs2_dir_entry *de; | 
 | 794 |  | 
| Joel Becker | b657c95 | 2008-11-13 14:49:11 -0800 | [diff] [blame] | 795 | 	ret = ocfs2_read_inode_block(inode, &di_bh); | 
| Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 796 | 	if (ret) { | 
 | 797 | 		mlog(ML_ERROR, "Unable to read inode block for dir %llu\n", | 
 | 798 | 		     (unsigned long long)OCFS2_I(inode)->ip_blkno); | 
 | 799 | 		goto out; | 
 | 800 | 	} | 
 | 801 |  | 
 | 802 | 	di = (struct ocfs2_dinode *)di_bh->b_data; | 
 | 803 | 	data = &di->id2.i_data; | 
 | 804 |  | 
 | 805 | 	while (*f_pos < i_size_read(inode)) { | 
 | 806 | revalidate: | 
 | 807 | 		/* If the dir block has changed since the last call to | 
 | 808 | 		 * readdir(2), then we might be pointing to an invalid | 
 | 809 | 		 * dirent right now.  Scan from the start of the block | 
 | 810 | 		 * to make sure. */ | 
 | 811 | 		if (*f_version != inode->i_version) { | 
 | 812 | 			for (i = 0; i < i_size_read(inode) && i < offset; ) { | 
 | 813 | 				de = (struct ocfs2_dir_entry *) | 
 | 814 | 					(data->id_data + i); | 
 | 815 | 				/* It's too expensive to do a full | 
 | 816 | 				 * dirent test each time round this | 
 | 817 | 				 * loop, but we do have to test at | 
 | 818 | 				 * least that it is non-zero.  A | 
 | 819 | 				 * failure will be detected in the | 
 | 820 | 				 * dirent test below. */ | 
 | 821 | 				if (le16_to_cpu(de->rec_len) < | 
 | 822 | 				    OCFS2_DIR_REC_LEN(1)) | 
 | 823 | 					break; | 
 | 824 | 				i += le16_to_cpu(de->rec_len); | 
 | 825 | 			} | 
 | 826 | 			*f_pos = offset = i; | 
 | 827 | 			*f_version = inode->i_version; | 
 | 828 | 		} | 
 | 829 |  | 
 | 830 | 		de = (struct ocfs2_dir_entry *) (data->id_data + *f_pos); | 
 | 831 | 		if (!ocfs2_check_dir_entry(inode, de, di_bh, *f_pos)) { | 
 | 832 | 			/* On error, skip the f_pos to the end. */ | 
 | 833 | 			*f_pos = i_size_read(inode); | 
 | 834 | 			goto out; | 
 | 835 | 		} | 
 | 836 | 		offset += le16_to_cpu(de->rec_len); | 
 | 837 | 		if (le64_to_cpu(de->inode)) { | 
 | 838 | 			/* We might block in the next section | 
 | 839 | 			 * if the data destination is | 
 | 840 | 			 * currently swapped out.  So, use a | 
 | 841 | 			 * version stamp to detect whether or | 
 | 842 | 			 * not the directory has been modified | 
 | 843 | 			 * during the copy operation. | 
 | 844 | 			 */ | 
| Mathieu Desnoyers | 2b47c36 | 2007-10-16 23:27:21 -0700 | [diff] [blame] | 845 | 			u64 version = *f_version; | 
| Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 846 | 			unsigned char d_type = DT_UNKNOWN; | 
 | 847 |  | 
 | 848 | 			if (de->file_type < OCFS2_FT_MAX) | 
 | 849 | 				d_type = ocfs2_filetype_table[de->file_type]; | 
 | 850 |  | 
 | 851 | 			filldir_ret = filldir(priv, de->name, | 
 | 852 | 					      de->name_len, | 
 | 853 | 					      *f_pos, | 
 | 854 | 					      le64_to_cpu(de->inode), | 
 | 855 | 					      d_type); | 
| Mark Fasheh | e7b3401 | 2007-09-24 14:25:27 -0700 | [diff] [blame] | 856 | 			if (filldir_ret) { | 
 | 857 | 				if (filldir_err) | 
 | 858 | 					*filldir_err = filldir_ret; | 
| Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 859 | 				break; | 
| Mark Fasheh | e7b3401 | 2007-09-24 14:25:27 -0700 | [diff] [blame] | 860 | 			} | 
| Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 861 | 			if (version != *f_version) | 
 | 862 | 				goto revalidate; | 
 | 863 | 		} | 
 | 864 | 		*f_pos += le16_to_cpu(de->rec_len); | 
 | 865 | 	} | 
 | 866 |  | 
 | 867 | out: | 
 | 868 | 	brelse(di_bh); | 
 | 869 |  | 
 | 870 | 	return 0; | 
 | 871 | } | 
 | 872 |  | 
 | 873 | static int ocfs2_dir_foreach_blk_el(struct inode *inode, | 
| Mathieu Desnoyers | 2b47c36 | 2007-10-16 23:27:21 -0700 | [diff] [blame] | 874 | 				    u64 *f_version, | 
| Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 875 | 				    loff_t *f_pos, void *priv, | 
| Mark Fasheh | e7b3401 | 2007-09-24 14:25:27 -0700 | [diff] [blame] | 876 | 				    filldir_t filldir, int *filldir_err) | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 877 | { | 
 | 878 | 	int error = 0; | 
| Mark Fasheh | aa95887 | 2006-04-21 13:49:02 -0700 | [diff] [blame] | 879 | 	unsigned long offset, blk, last_ra_blk = 0; | 
 | 880 | 	int i, stored; | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 881 | 	struct buffer_head * bh, * tmp; | 
 | 882 | 	struct ocfs2_dir_entry * de; | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 883 | 	struct super_block * sb = inode->i_sb; | 
| Mark Fasheh | aa95887 | 2006-04-21 13:49:02 -0700 | [diff] [blame] | 884 | 	unsigned int ra_sectors = 16; | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 885 |  | 
 | 886 | 	stored = 0; | 
 | 887 | 	bh = NULL; | 
 | 888 |  | 
| Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 889 | 	offset = (*f_pos) & (sb->s_blocksize - 1); | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 890 |  | 
| Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 891 | 	while (!error && !stored && *f_pos < i_size_read(inode)) { | 
 | 892 | 		blk = (*f_pos) >> sb->s_blocksize_bits; | 
| Joel Becker | a22305c | 2008-11-13 14:49:17 -0800 | [diff] [blame] | 893 | 		if (ocfs2_read_dir_block(inode, blk, &bh, 0)) { | 
 | 894 | 			/* Skip the corrupt dirblock and keep trying */ | 
| Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 895 | 			*f_pos += sb->s_blocksize - offset; | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 896 | 			continue; | 
 | 897 | 		} | 
 | 898 |  | 
| Mark Fasheh | aa95887 | 2006-04-21 13:49:02 -0700 | [diff] [blame] | 899 | 		/* The idea here is to begin with 8k read-ahead and to stay | 
 | 900 | 		 * 4k ahead of our current position. | 
 | 901 | 		 * | 
 | 902 | 		 * TODO: Use the pagecache for this. We just need to | 
 | 903 | 		 * make sure it's cluster-safe... */ | 
 | 904 | 		if (!last_ra_blk | 
 | 905 | 		    || (((last_ra_blk - blk) << 9) <= (ra_sectors / 2))) { | 
 | 906 | 			for (i = ra_sectors >> (sb->s_blocksize_bits - 9); | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 907 | 			     i > 0; i--) { | 
| Joel Becker | a22305c | 2008-11-13 14:49:17 -0800 | [diff] [blame] | 908 | 				tmp = NULL; | 
 | 909 | 				if (!ocfs2_read_dir_block(inode, ++blk, &tmp, | 
 | 910 | 							  OCFS2_BH_READAHEAD)) | 
 | 911 | 					brelse(tmp); | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 912 | 			} | 
| Mark Fasheh | aa95887 | 2006-04-21 13:49:02 -0700 | [diff] [blame] | 913 | 			last_ra_blk = blk; | 
 | 914 | 			ra_sectors = 8; | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 915 | 		} | 
 | 916 |  | 
 | 917 | revalidate: | 
 | 918 | 		/* If the dir block has changed since the last call to | 
 | 919 | 		 * readdir(2), then we might be pointing to an invalid | 
 | 920 | 		 * dirent right now.  Scan from the start of the block | 
 | 921 | 		 * to make sure. */ | 
| Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 922 | 		if (*f_version != inode->i_version) { | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 923 | 			for (i = 0; i < sb->s_blocksize && i < offset; ) { | 
 | 924 | 				de = (struct ocfs2_dir_entry *) (bh->b_data + i); | 
 | 925 | 				/* It's too expensive to do a full | 
 | 926 | 				 * dirent test each time round this | 
 | 927 | 				 * loop, but we do have to test at | 
 | 928 | 				 * least that it is non-zero.  A | 
 | 929 | 				 * failure will be detected in the | 
 | 930 | 				 * dirent test below. */ | 
 | 931 | 				if (le16_to_cpu(de->rec_len) < | 
 | 932 | 				    OCFS2_DIR_REC_LEN(1)) | 
 | 933 | 					break; | 
 | 934 | 				i += le16_to_cpu(de->rec_len); | 
 | 935 | 			} | 
 | 936 | 			offset = i; | 
| Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 937 | 			*f_pos = ((*f_pos) & ~(sb->s_blocksize - 1)) | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 938 | 				| offset; | 
| Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 939 | 			*f_version = inode->i_version; | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 940 | 		} | 
 | 941 |  | 
| Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 942 | 		while (!error && *f_pos < i_size_read(inode) | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 943 | 		       && offset < sb->s_blocksize) { | 
 | 944 | 			de = (struct ocfs2_dir_entry *) (bh->b_data + offset); | 
 | 945 | 			if (!ocfs2_check_dir_entry(inode, de, bh, offset)) { | 
 | 946 | 				/* On error, skip the f_pos to the | 
 | 947 | 				   next block. */ | 
| Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 948 | 				*f_pos = ((*f_pos) | (sb->s_blocksize - 1)) + 1; | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 949 | 				brelse(bh); | 
| Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 950 | 				goto out; | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 951 | 			} | 
 | 952 | 			offset += le16_to_cpu(de->rec_len); | 
 | 953 | 			if (le64_to_cpu(de->inode)) { | 
 | 954 | 				/* We might block in the next section | 
 | 955 | 				 * if the data destination is | 
 | 956 | 				 * currently swapped out.  So, use a | 
 | 957 | 				 * version stamp to detect whether or | 
 | 958 | 				 * not the directory has been modified | 
 | 959 | 				 * during the copy operation. | 
 | 960 | 				 */ | 
| Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 961 | 				unsigned long version = *f_version; | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 962 | 				unsigned char d_type = DT_UNKNOWN; | 
 | 963 |  | 
 | 964 | 				if (de->file_type < OCFS2_FT_MAX) | 
 | 965 | 					d_type = ocfs2_filetype_table[de->file_type]; | 
| Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 966 | 				error = filldir(priv, de->name, | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 967 | 						de->name_len, | 
| Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 968 | 						*f_pos, | 
| Mark Fasheh | 7e85367 | 2007-09-10 17:30:26 -0700 | [diff] [blame] | 969 | 						le64_to_cpu(de->inode), | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 970 | 						d_type); | 
| Mark Fasheh | e7b3401 | 2007-09-24 14:25:27 -0700 | [diff] [blame] | 971 | 				if (error) { | 
 | 972 | 					if (filldir_err) | 
 | 973 | 						*filldir_err = error; | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 974 | 					break; | 
| Mark Fasheh | e7b3401 | 2007-09-24 14:25:27 -0700 | [diff] [blame] | 975 | 				} | 
| Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 976 | 				if (version != *f_version) | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 977 | 					goto revalidate; | 
 | 978 | 				stored ++; | 
 | 979 | 			} | 
| Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 980 | 			*f_pos += le16_to_cpu(de->rec_len); | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 981 | 		} | 
 | 982 | 		offset = 0; | 
 | 983 | 		brelse(bh); | 
| Joel Becker | a22305c | 2008-11-13 14:49:17 -0800 | [diff] [blame] | 984 | 		bh = NULL; | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 985 | 	} | 
 | 986 |  | 
 | 987 | 	stored = 0; | 
| Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 988 | out: | 
 | 989 | 	return stored; | 
 | 990 | } | 
 | 991 |  | 
| Mathieu Desnoyers | 2b47c36 | 2007-10-16 23:27:21 -0700 | [diff] [blame] | 992 | static int ocfs2_dir_foreach_blk(struct inode *inode, u64 *f_version, | 
| Mark Fasheh | e7b3401 | 2007-09-24 14:25:27 -0700 | [diff] [blame] | 993 | 				 loff_t *f_pos, void *priv, filldir_t filldir, | 
 | 994 | 				 int *filldir_err) | 
| Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 995 | { | 
 | 996 | 	if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) | 
 | 997 | 		return ocfs2_dir_foreach_blk_id(inode, f_version, f_pos, priv, | 
| Mark Fasheh | e7b3401 | 2007-09-24 14:25:27 -0700 | [diff] [blame] | 998 | 						filldir, filldir_err); | 
| Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 999 |  | 
| Mark Fasheh | e7b3401 | 2007-09-24 14:25:27 -0700 | [diff] [blame] | 1000 | 	return ocfs2_dir_foreach_blk_el(inode, f_version, f_pos, priv, filldir, | 
 | 1001 | 					filldir_err); | 
| Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 1002 | } | 
 | 1003 |  | 
| Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 1004 | /* | 
| Mark Fasheh | 5eae5b9 | 2007-09-10 17:50:51 -0700 | [diff] [blame] | 1005 |  * This is intended to be called from inside other kernel functions, | 
 | 1006 |  * so we fake some arguments. | 
 | 1007 |  */ | 
 | 1008 | int ocfs2_dir_foreach(struct inode *inode, loff_t *f_pos, void *priv, | 
 | 1009 | 		      filldir_t filldir) | 
 | 1010 | { | 
| Mark Fasheh | e7b3401 | 2007-09-24 14:25:27 -0700 | [diff] [blame] | 1011 | 	int ret = 0, filldir_err = 0; | 
| Mathieu Desnoyers | 2b47c36 | 2007-10-16 23:27:21 -0700 | [diff] [blame] | 1012 | 	u64 version = inode->i_version; | 
| Mark Fasheh | 5eae5b9 | 2007-09-10 17:50:51 -0700 | [diff] [blame] | 1013 |  | 
 | 1014 | 	while (*f_pos < i_size_read(inode)) { | 
 | 1015 | 		ret = ocfs2_dir_foreach_blk(inode, &version, f_pos, priv, | 
| Mark Fasheh | e7b3401 | 2007-09-24 14:25:27 -0700 | [diff] [blame] | 1016 | 					    filldir, &filldir_err); | 
 | 1017 | 		if (ret || filldir_err) | 
| Mark Fasheh | 5eae5b9 | 2007-09-10 17:50:51 -0700 | [diff] [blame] | 1018 | 			break; | 
 | 1019 | 	} | 
 | 1020 |  | 
| Mark Fasheh | e7b3401 | 2007-09-24 14:25:27 -0700 | [diff] [blame] | 1021 | 	if (ret > 0) | 
 | 1022 | 		ret = -EIO; | 
 | 1023 |  | 
| Mark Fasheh | 5eae5b9 | 2007-09-10 17:50:51 -0700 | [diff] [blame] | 1024 | 	return 0; | 
 | 1025 | } | 
 | 1026 |  | 
 | 1027 | /* | 
| Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 1028 |  * ocfs2_readdir() | 
 | 1029 |  * | 
 | 1030 |  */ | 
 | 1031 | int ocfs2_readdir(struct file * filp, void * dirent, filldir_t filldir) | 
 | 1032 | { | 
 | 1033 | 	int error = 0; | 
 | 1034 | 	struct inode *inode = filp->f_path.dentry->d_inode; | 
 | 1035 | 	int lock_level = 0; | 
 | 1036 |  | 
 | 1037 | 	mlog_entry("dirino=%llu\n", | 
 | 1038 | 		   (unsigned long long)OCFS2_I(inode)->ip_blkno); | 
 | 1039 |  | 
| Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 1040 | 	error = ocfs2_inode_lock_atime(inode, filp->f_vfsmnt, &lock_level); | 
| Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 1041 | 	if (lock_level && error >= 0) { | 
 | 1042 | 		/* We release EX lock which used to update atime | 
 | 1043 | 		 * and get PR lock again to reduce contention | 
 | 1044 | 		 * on commonly accessed directories. */ | 
| Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 1045 | 		ocfs2_inode_unlock(inode, 1); | 
| Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 1046 | 		lock_level = 0; | 
| Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 1047 | 		error = ocfs2_inode_lock(inode, NULL, 0); | 
| Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 1048 | 	} | 
 | 1049 | 	if (error < 0) { | 
 | 1050 | 		if (error != -ENOENT) | 
 | 1051 | 			mlog_errno(error); | 
 | 1052 | 		/* we haven't got any yet, so propagate the error. */ | 
 | 1053 | 		goto bail_nolock; | 
 | 1054 | 	} | 
 | 1055 |  | 
 | 1056 | 	error = ocfs2_dir_foreach_blk(inode, &filp->f_version, &filp->f_pos, | 
| Mark Fasheh | e7b3401 | 2007-09-24 14:25:27 -0700 | [diff] [blame] | 1057 | 				      dirent, filldir, NULL); | 
| Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 1058 |  | 
| Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 1059 | 	ocfs2_inode_unlock(inode, lock_level); | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1060 |  | 
| Mark Fasheh | aa95887 | 2006-04-21 13:49:02 -0700 | [diff] [blame] | 1061 | bail_nolock: | 
| Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 1062 | 	mlog_exit(error); | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1063 |  | 
| Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 1064 | 	return error; | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1065 | } | 
 | 1066 |  | 
 | 1067 | /* | 
| Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 1068 |  * NOTE: this should always be called with parent dir i_mutex taken. | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1069 |  */ | 
 | 1070 | int ocfs2_find_files_on_disk(const char *name, | 
 | 1071 | 			     int namelen, | 
 | 1072 | 			     u64 *blkno, | 
 | 1073 | 			     struct inode *inode, | 
 | 1074 | 			     struct buffer_head **dirent_bh, | 
 | 1075 | 			     struct ocfs2_dir_entry **dirent) | 
 | 1076 | { | 
 | 1077 | 	int status = -ENOENT; | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1078 |  | 
| Joel Becker | 2b388c6 | 2006-05-10 18:28:59 -0700 | [diff] [blame] | 1079 | 	mlog_entry("(name=%.*s, blkno=%p, inode=%p, dirent_bh=%p, dirent=%p)\n", | 
 | 1080 | 		   namelen, name, blkno, inode, dirent_bh, dirent); | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1081 |  | 
 | 1082 | 	*dirent_bh = ocfs2_find_entry(name, namelen, inode, dirent); | 
 | 1083 | 	if (!*dirent_bh || !*dirent) { | 
 | 1084 | 		status = -ENOENT; | 
 | 1085 | 		goto leave; | 
 | 1086 | 	} | 
 | 1087 |  | 
 | 1088 | 	*blkno = le64_to_cpu((*dirent)->inode); | 
 | 1089 |  | 
 | 1090 | 	status = 0; | 
 | 1091 | leave: | 
 | 1092 | 	if (status < 0) { | 
 | 1093 | 		*dirent = NULL; | 
| Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 1094 | 		brelse(*dirent_bh); | 
 | 1095 | 		*dirent_bh = NULL; | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1096 | 	} | 
 | 1097 |  | 
 | 1098 | 	mlog_exit(status); | 
 | 1099 | 	return status; | 
 | 1100 | } | 
 | 1101 |  | 
| Mark Fasheh | be94d11 | 2007-09-11 15:22:06 -0700 | [diff] [blame] | 1102 | /* | 
 | 1103 |  * Convenience function for callers which just want the block number | 
 | 1104 |  * mapped to a name and don't require the full dirent info, etc. | 
 | 1105 |  */ | 
 | 1106 | int ocfs2_lookup_ino_from_name(struct inode *dir, const char *name, | 
 | 1107 | 			       int namelen, u64 *blkno) | 
 | 1108 | { | 
 | 1109 | 	int ret; | 
 | 1110 | 	struct buffer_head *bh = NULL; | 
 | 1111 | 	struct ocfs2_dir_entry *dirent = NULL; | 
 | 1112 |  | 
 | 1113 | 	ret = ocfs2_find_files_on_disk(name, namelen, blkno, dir, &bh, &dirent); | 
 | 1114 | 	brelse(bh); | 
 | 1115 |  | 
 | 1116 | 	return ret; | 
 | 1117 | } | 
 | 1118 |  | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1119 | /* Check for a name within a directory. | 
 | 1120 |  * | 
 | 1121 |  * Return 0 if the name does not exist | 
 | 1122 |  * Return -EEXIST if the directory contains the name | 
 | 1123 |  * | 
| Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 1124 |  * Callers should have i_mutex + a cluster lock on dir | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1125 |  */ | 
 | 1126 | int ocfs2_check_dir_for_entry(struct inode *dir, | 
 | 1127 | 			      const char *name, | 
 | 1128 | 			      int namelen) | 
 | 1129 | { | 
 | 1130 | 	int ret; | 
 | 1131 | 	struct buffer_head *dirent_bh = NULL; | 
 | 1132 | 	struct ocfs2_dir_entry *dirent = NULL; | 
 | 1133 |  | 
| Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 1134 | 	mlog_entry("dir %llu, name '%.*s'\n", | 
 | 1135 | 		   (unsigned long long)OCFS2_I(dir)->ip_blkno, namelen, name); | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1136 |  | 
 | 1137 | 	ret = -EEXIST; | 
 | 1138 | 	dirent_bh = ocfs2_find_entry(name, namelen, dir, &dirent); | 
 | 1139 | 	if (dirent_bh) | 
 | 1140 | 		goto bail; | 
 | 1141 |  | 
 | 1142 | 	ret = 0; | 
 | 1143 | bail: | 
| Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 1144 | 	brelse(dirent_bh); | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1145 |  | 
 | 1146 | 	mlog_exit(ret); | 
 | 1147 | 	return ret; | 
 | 1148 | } | 
 | 1149 |  | 
| Mark Fasheh | 0bfbbf6 | 2007-09-12 11:19:00 -0700 | [diff] [blame] | 1150 | struct ocfs2_empty_dir_priv { | 
 | 1151 | 	unsigned seen_dot; | 
 | 1152 | 	unsigned seen_dot_dot; | 
 | 1153 | 	unsigned seen_other; | 
 | 1154 | }; | 
 | 1155 | static int ocfs2_empty_dir_filldir(void *priv, const char *name, int name_len, | 
 | 1156 | 				   loff_t pos, u64 ino, unsigned type) | 
 | 1157 | { | 
 | 1158 | 	struct ocfs2_empty_dir_priv *p = priv; | 
 | 1159 |  | 
 | 1160 | 	/* | 
 | 1161 | 	 * Check the positions of "." and ".." records to be sure | 
 | 1162 | 	 * they're in the correct place. | 
 | 1163 | 	 */ | 
 | 1164 | 	if (name_len == 1 && !strncmp(".", name, 1) && pos == 0) { | 
 | 1165 | 		p->seen_dot = 1; | 
 | 1166 | 		return 0; | 
 | 1167 | 	} | 
 | 1168 |  | 
 | 1169 | 	if (name_len == 2 && !strncmp("..", name, 2) && | 
 | 1170 | 	    pos == OCFS2_DIR_REC_LEN(1)) { | 
 | 1171 | 		p->seen_dot_dot = 1; | 
 | 1172 | 		return 0; | 
 | 1173 | 	} | 
 | 1174 |  | 
 | 1175 | 	p->seen_other = 1; | 
 | 1176 | 	return 1; | 
 | 1177 | } | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1178 | /* | 
 | 1179 |  * routine to check that the specified directory is empty (for rmdir) | 
| Mark Fasheh | 0bfbbf6 | 2007-09-12 11:19:00 -0700 | [diff] [blame] | 1180 |  * | 
 | 1181 |  * Returns 1 if dir is empty, zero otherwise. | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1182 |  */ | 
 | 1183 | int ocfs2_empty_dir(struct inode *inode) | 
 | 1184 | { | 
| Mark Fasheh | 0bfbbf6 | 2007-09-12 11:19:00 -0700 | [diff] [blame] | 1185 | 	int ret; | 
 | 1186 | 	loff_t start = 0; | 
 | 1187 | 	struct ocfs2_empty_dir_priv priv; | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1188 |  | 
| Mark Fasheh | 0bfbbf6 | 2007-09-12 11:19:00 -0700 | [diff] [blame] | 1189 | 	memset(&priv, 0, sizeof(priv)); | 
 | 1190 |  | 
 | 1191 | 	ret = ocfs2_dir_foreach(inode, &start, &priv, ocfs2_empty_dir_filldir); | 
 | 1192 | 	if (ret) | 
 | 1193 | 		mlog_errno(ret); | 
 | 1194 |  | 
 | 1195 | 	if (!priv.seen_dot || !priv.seen_dot_dot) { | 
 | 1196 | 		mlog(ML_ERROR, "bad directory (dir #%llu) - no `.' or `..'\n", | 
| Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 1197 | 		     (unsigned long long)OCFS2_I(inode)->ip_blkno); | 
| Mark Fasheh | 0bfbbf6 | 2007-09-12 11:19:00 -0700 | [diff] [blame] | 1198 | 		/* | 
 | 1199 | 		 * XXX: Is it really safe to allow an unlink to continue? | 
 | 1200 | 		 */ | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1201 | 		return 1; | 
 | 1202 | 	} | 
 | 1203 |  | 
| Mark Fasheh | 0bfbbf6 | 2007-09-12 11:19:00 -0700 | [diff] [blame] | 1204 | 	return !priv.seen_other; | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1205 | } | 
 | 1206 |  | 
| Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 1207 | /* | 
 | 1208 |  * Fills "." and ".." dirents in a new directory block. Returns dirent for | 
 | 1209 |  * "..", which might be used during creation of a directory with a trailing | 
 | 1210 |  * header. It is otherwise safe to ignore the return code. | 
 | 1211 |  */ | 
 | 1212 | static struct ocfs2_dir_entry *ocfs2_fill_initial_dirents(struct inode *inode, | 
 | 1213 | 							  struct inode *parent, | 
 | 1214 | 							  char *start, | 
 | 1215 | 							  unsigned int size) | 
| Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1216 | { | 
 | 1217 | 	struct ocfs2_dir_entry *de = (struct ocfs2_dir_entry *)start; | 
 | 1218 |  | 
 | 1219 | 	de->inode = cpu_to_le64(OCFS2_I(inode)->ip_blkno); | 
 | 1220 | 	de->name_len = 1; | 
 | 1221 | 	de->rec_len = | 
 | 1222 | 		cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len)); | 
 | 1223 | 	strcpy(de->name, "."); | 
 | 1224 | 	ocfs2_set_de_type(de, S_IFDIR); | 
 | 1225 |  | 
 | 1226 | 	de = (struct ocfs2_dir_entry *) ((char *)de + le16_to_cpu(de->rec_len)); | 
 | 1227 | 	de->inode = cpu_to_le64(OCFS2_I(parent)->ip_blkno); | 
 | 1228 | 	de->rec_len = cpu_to_le16(size - OCFS2_DIR_REC_LEN(1)); | 
 | 1229 | 	de->name_len = 2; | 
 | 1230 | 	strcpy(de->name, ".."); | 
 | 1231 | 	ocfs2_set_de_type(de, S_IFDIR); | 
| Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 1232 |  | 
 | 1233 | 	return de; | 
| Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1234 | } | 
 | 1235 |  | 
 | 1236 | /* | 
 | 1237 |  * This works together with code in ocfs2_mknod_locked() which sets | 
 | 1238 |  * the inline-data flag and initializes the inline-data section. | 
 | 1239 |  */ | 
 | 1240 | static int ocfs2_fill_new_dir_id(struct ocfs2_super *osb, | 
 | 1241 | 				 handle_t *handle, | 
 | 1242 | 				 struct inode *parent, | 
 | 1243 | 				 struct inode *inode, | 
 | 1244 | 				 struct buffer_head *di_bh) | 
 | 1245 | { | 
 | 1246 | 	int ret; | 
 | 1247 | 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; | 
 | 1248 | 	struct ocfs2_inline_data *data = &di->id2.i_data; | 
 | 1249 | 	unsigned int size = le16_to_cpu(data->id_count); | 
 | 1250 |  | 
| Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1251 | 	ret = ocfs2_journal_access_di(handle, inode, di_bh, | 
 | 1252 | 				      OCFS2_JOURNAL_ACCESS_WRITE); | 
| Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1253 | 	if (ret) { | 
 | 1254 | 		mlog_errno(ret); | 
 | 1255 | 		goto out; | 
 | 1256 | 	} | 
 | 1257 |  | 
 | 1258 | 	ocfs2_fill_initial_dirents(inode, parent, data->id_data, size); | 
 | 1259 |  | 
 | 1260 | 	ocfs2_journal_dirty(handle, di_bh); | 
 | 1261 | 	if (ret) { | 
 | 1262 | 		mlog_errno(ret); | 
 | 1263 | 		goto out; | 
 | 1264 | 	} | 
 | 1265 |  | 
 | 1266 | 	i_size_write(inode, size); | 
 | 1267 | 	inode->i_nlink = 2; | 
 | 1268 | 	inode->i_blocks = ocfs2_inode_sector_count(inode); | 
 | 1269 |  | 
 | 1270 | 	ret = ocfs2_mark_inode_dirty(handle, inode, di_bh); | 
 | 1271 | 	if (ret < 0) | 
 | 1272 | 		mlog_errno(ret); | 
 | 1273 |  | 
 | 1274 | out: | 
 | 1275 | 	return ret; | 
 | 1276 | } | 
 | 1277 |  | 
 | 1278 | static int ocfs2_fill_new_dir_el(struct ocfs2_super *osb, | 
 | 1279 | 				 handle_t *handle, | 
 | 1280 | 				 struct inode *parent, | 
 | 1281 | 				 struct inode *inode, | 
 | 1282 | 				 struct buffer_head *fe_bh, | 
 | 1283 | 				 struct ocfs2_alloc_context *data_ac) | 
| Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 1284 | { | 
 | 1285 | 	int status; | 
| Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 1286 | 	unsigned int size = osb->sb->s_blocksize; | 
| Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 1287 | 	struct buffer_head *new_bh = NULL; | 
| Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 1288 | 	struct ocfs2_dir_entry *de; | 
| Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 1289 |  | 
 | 1290 | 	mlog_entry_void(); | 
 | 1291 |  | 
| Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 1292 | 	if (ocfs2_supports_dir_trailer(osb)) | 
 | 1293 | 		size = ocfs2_dir_trailer_blk_off(parent->i_sb); | 
 | 1294 |  | 
| Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 1295 | 	status = ocfs2_do_extend_dir(osb->sb, handle, inode, fe_bh, | 
 | 1296 | 				     data_ac, NULL, &new_bh); | 
 | 1297 | 	if (status < 0) { | 
 | 1298 | 		mlog_errno(status); | 
 | 1299 | 		goto bail; | 
 | 1300 | 	} | 
 | 1301 |  | 
 | 1302 | 	ocfs2_set_new_buffer_uptodate(inode, new_bh); | 
 | 1303 |  | 
| Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1304 | 	status = ocfs2_journal_access_db(handle, inode, new_bh, | 
 | 1305 | 					 OCFS2_JOURNAL_ACCESS_CREATE); | 
| Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 1306 | 	if (status < 0) { | 
 | 1307 | 		mlog_errno(status); | 
 | 1308 | 		goto bail; | 
 | 1309 | 	} | 
 | 1310 | 	memset(new_bh->b_data, 0, osb->sb->s_blocksize); | 
 | 1311 |  | 
| Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 1312 | 	de = ocfs2_fill_initial_dirents(inode, parent, new_bh->b_data, size); | 
 | 1313 | 	if (ocfs2_supports_dir_trailer(osb)) | 
 | 1314 | 		ocfs2_init_dir_trailer(inode, new_bh); | 
| Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 1315 |  | 
 | 1316 | 	status = ocfs2_journal_dirty(handle, new_bh); | 
 | 1317 | 	if (status < 0) { | 
 | 1318 | 		mlog_errno(status); | 
 | 1319 | 		goto bail; | 
 | 1320 | 	} | 
 | 1321 |  | 
 | 1322 | 	i_size_write(inode, inode->i_sb->s_blocksize); | 
 | 1323 | 	inode->i_nlink = 2; | 
 | 1324 | 	inode->i_blocks = ocfs2_inode_sector_count(inode); | 
 | 1325 | 	status = ocfs2_mark_inode_dirty(handle, inode, fe_bh); | 
 | 1326 | 	if (status < 0) { | 
 | 1327 | 		mlog_errno(status); | 
 | 1328 | 		goto bail; | 
 | 1329 | 	} | 
 | 1330 |  | 
 | 1331 | 	status = 0; | 
 | 1332 | bail: | 
| Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 1333 | 	brelse(new_bh); | 
| Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 1334 |  | 
 | 1335 | 	mlog_exit(status); | 
 | 1336 | 	return status; | 
 | 1337 | } | 
 | 1338 |  | 
| Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1339 | int ocfs2_fill_new_dir(struct ocfs2_super *osb, | 
 | 1340 | 		       handle_t *handle, | 
 | 1341 | 		       struct inode *parent, | 
 | 1342 | 		       struct inode *inode, | 
 | 1343 | 		       struct buffer_head *fe_bh, | 
 | 1344 | 		       struct ocfs2_alloc_context *data_ac) | 
 | 1345 | { | 
 | 1346 | 	BUG_ON(!ocfs2_supports_inline_data(osb) && data_ac == NULL); | 
 | 1347 |  | 
 | 1348 | 	if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) | 
 | 1349 | 		return ocfs2_fill_new_dir_id(osb, handle, parent, inode, fe_bh); | 
 | 1350 |  | 
 | 1351 | 	return ocfs2_fill_new_dir_el(osb, handle, parent, inode, fe_bh, | 
 | 1352 | 				     data_ac); | 
 | 1353 | } | 
 | 1354 |  | 
| Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 1355 | /* | 
 | 1356 |  * Expand rec_len of the rightmost dirent in a directory block so that it | 
 | 1357 |  * contains the end of our valid space for dirents. We do this during | 
 | 1358 |  * expansion from an inline directory to one with extents. The first dir block | 
 | 1359 |  * in that case is taken from the inline data portion of the inode block. | 
 | 1360 |  * | 
 | 1361 |  * We add the dir trailer if this filesystem wants it. | 
 | 1362 |  */ | 
| Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1363 | static void ocfs2_expand_last_dirent(char *start, unsigned int old_size, | 
| Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 1364 | 				     struct super_block *sb) | 
| Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1365 | { | 
 | 1366 | 	struct ocfs2_dir_entry *de; | 
 | 1367 | 	struct ocfs2_dir_entry *prev_de; | 
 | 1368 | 	char *de_buf, *limit; | 
| Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 1369 | 	unsigned int new_size = sb->s_blocksize; | 
 | 1370 | 	unsigned int bytes; | 
 | 1371 |  | 
 | 1372 | 	if (ocfs2_supports_dir_trailer(OCFS2_SB(sb))) | 
 | 1373 | 		new_size = ocfs2_dir_trailer_blk_off(sb); | 
 | 1374 |  | 
 | 1375 | 	bytes = new_size - old_size; | 
| Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1376 |  | 
 | 1377 | 	limit = start + old_size; | 
 | 1378 | 	de_buf = start; | 
 | 1379 | 	de = (struct ocfs2_dir_entry *)de_buf; | 
 | 1380 | 	do { | 
 | 1381 | 		prev_de = de; | 
 | 1382 | 		de_buf += le16_to_cpu(de->rec_len); | 
 | 1383 | 		de = (struct ocfs2_dir_entry *)de_buf; | 
 | 1384 | 	} while (de_buf < limit); | 
 | 1385 |  | 
 | 1386 | 	le16_add_cpu(&prev_de->rec_len, bytes); | 
 | 1387 | } | 
 | 1388 |  | 
 | 1389 | /* | 
 | 1390 |  * We allocate enough clusters to fulfill "blocks_wanted", but set | 
 | 1391 |  * i_size to exactly one block. Ocfs2_extend_dir() will handle the | 
 | 1392 |  * rest automatically for us. | 
 | 1393 |  * | 
 | 1394 |  * *first_block_bh is a pointer to the 1st data block allocated to the | 
 | 1395 |  *  directory. | 
 | 1396 |  */ | 
 | 1397 | static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh, | 
 | 1398 | 				   unsigned int blocks_wanted, | 
 | 1399 | 				   struct buffer_head **first_block_bh) | 
 | 1400 | { | 
| Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1401 | 	u32 alloc, bit_off, len; | 
 | 1402 | 	struct super_block *sb = dir->i_sb; | 
| Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 1403 | 	int ret, credits = ocfs2_inline_to_extents_credits(sb); | 
| Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1404 | 	u64 blkno, bytes = blocks_wanted << sb->s_blocksize_bits; | 
 | 1405 | 	struct ocfs2_super *osb = OCFS2_SB(dir->i_sb); | 
 | 1406 | 	struct ocfs2_inode_info *oi = OCFS2_I(dir); | 
 | 1407 | 	struct ocfs2_alloc_context *data_ac; | 
 | 1408 | 	struct buffer_head *dirdata_bh = NULL; | 
 | 1409 | 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; | 
 | 1410 | 	handle_t *handle; | 
| Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 1411 | 	struct ocfs2_extent_tree et; | 
| Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 1412 | 	int did_quota = 0; | 
| Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 1413 |  | 
| Joel Becker | 8d6220d | 2008-08-22 12:46:09 -0700 | [diff] [blame] | 1414 | 	ocfs2_init_dinode_extent_tree(&et, dir, di_bh); | 
| Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1415 |  | 
 | 1416 | 	alloc = ocfs2_clusters_for_bytes(sb, bytes); | 
 | 1417 |  | 
 | 1418 | 	/* | 
 | 1419 | 	 * We should never need more than 2 clusters for this - | 
 | 1420 | 	 * maximum dirent size is far less than one block. In fact, | 
 | 1421 | 	 * the only time we'd need more than one cluster is if | 
 | 1422 | 	 * blocksize == clustersize and the dirent won't fit in the | 
 | 1423 | 	 * extra space that the expansion to a single block gives. As | 
 | 1424 | 	 * of today, that only happens on 4k/4k file systems. | 
 | 1425 | 	 */ | 
 | 1426 | 	BUG_ON(alloc > 2); | 
 | 1427 |  | 
 | 1428 | 	ret = ocfs2_reserve_clusters(osb, alloc, &data_ac); | 
 | 1429 | 	if (ret) { | 
 | 1430 | 		mlog_errno(ret); | 
 | 1431 | 		goto out; | 
 | 1432 | 	} | 
 | 1433 |  | 
 | 1434 | 	down_write(&oi->ip_alloc_sem); | 
 | 1435 |  | 
 | 1436 | 	/* | 
| Joe Perches | c78bad1 | 2008-02-03 17:33:42 +0200 | [diff] [blame] | 1437 | 	 * Prepare for worst case allocation scenario of two separate | 
| Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1438 | 	 * extents. | 
 | 1439 | 	 */ | 
 | 1440 | 	if (alloc == 2) | 
 | 1441 | 		credits += OCFS2_SUBALLOC_ALLOC; | 
 | 1442 |  | 
 | 1443 | 	handle = ocfs2_start_trans(osb, credits); | 
 | 1444 | 	if (IS_ERR(handle)) { | 
 | 1445 | 		ret = PTR_ERR(handle); | 
 | 1446 | 		mlog_errno(ret); | 
 | 1447 | 		goto out_sem; | 
 | 1448 | 	} | 
 | 1449 |  | 
| Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 1450 | 	if (vfs_dq_alloc_space_nodirty(dir, | 
 | 1451 | 				ocfs2_clusters_to_bytes(osb->sb, alloc))) { | 
 | 1452 | 		ret = -EDQUOT; | 
 | 1453 | 		goto out_commit; | 
 | 1454 | 	} | 
 | 1455 | 	did_quota = 1; | 
| Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1456 | 	/* | 
 | 1457 | 	 * Try to claim as many clusters as the bitmap can give though | 
 | 1458 | 	 * if we only get one now, that's enough to continue. The rest | 
 | 1459 | 	 * will be claimed after the conversion to extents. | 
 | 1460 | 	 */ | 
 | 1461 | 	ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off, &len); | 
 | 1462 | 	if (ret) { | 
 | 1463 | 		mlog_errno(ret); | 
 | 1464 | 		goto out_commit; | 
 | 1465 | 	} | 
 | 1466 |  | 
 | 1467 | 	/* | 
 | 1468 | 	 * Operations are carefully ordered so that we set up the new | 
 | 1469 | 	 * data block first. The conversion from inline data to | 
 | 1470 | 	 * extents follows. | 
 | 1471 | 	 */ | 
 | 1472 | 	blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off); | 
 | 1473 | 	dirdata_bh = sb_getblk(sb, blkno); | 
 | 1474 | 	if (!dirdata_bh) { | 
 | 1475 | 		ret = -EIO; | 
 | 1476 | 		mlog_errno(ret); | 
 | 1477 | 		goto out_commit; | 
 | 1478 | 	} | 
 | 1479 |  | 
 | 1480 | 	ocfs2_set_new_buffer_uptodate(dir, dirdata_bh); | 
 | 1481 |  | 
| Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1482 | 	ret = ocfs2_journal_access_db(handle, dir, dirdata_bh, | 
 | 1483 | 				      OCFS2_JOURNAL_ACCESS_CREATE); | 
| Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1484 | 	if (ret) { | 
 | 1485 | 		mlog_errno(ret); | 
 | 1486 | 		goto out_commit; | 
 | 1487 | 	} | 
 | 1488 |  | 
 | 1489 | 	memcpy(dirdata_bh->b_data, di->id2.i_data.id_data, i_size_read(dir)); | 
 | 1490 | 	memset(dirdata_bh->b_data + i_size_read(dir), 0, | 
 | 1491 | 	       sb->s_blocksize - i_size_read(dir)); | 
| Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 1492 | 	ocfs2_expand_last_dirent(dirdata_bh->b_data, i_size_read(dir), sb); | 
 | 1493 | 	if (ocfs2_supports_dir_trailer(osb)) | 
 | 1494 | 		ocfs2_init_dir_trailer(dir, dirdata_bh); | 
| Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1495 |  | 
 | 1496 | 	ret = ocfs2_journal_dirty(handle, dirdata_bh); | 
 | 1497 | 	if (ret) { | 
 | 1498 | 		mlog_errno(ret); | 
 | 1499 | 		goto out_commit; | 
 | 1500 | 	} | 
 | 1501 |  | 
 | 1502 | 	/* | 
 | 1503 | 	 * Set extent, i_size, etc on the directory. After this, the | 
 | 1504 | 	 * inode should contain the same exact dirents as before and | 
 | 1505 | 	 * be fully accessible from system calls. | 
 | 1506 | 	 * | 
 | 1507 | 	 * We let the later dirent insert modify c/mtime - to the user | 
 | 1508 | 	 * the data hasn't changed. | 
 | 1509 | 	 */ | 
| Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1510 | 	ret = ocfs2_journal_access_di(handle, dir, di_bh, | 
 | 1511 | 				      OCFS2_JOURNAL_ACCESS_CREATE); | 
| Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1512 | 	if (ret) { | 
 | 1513 | 		mlog_errno(ret); | 
 | 1514 | 		goto out_commit; | 
 | 1515 | 	} | 
 | 1516 |  | 
 | 1517 | 	spin_lock(&oi->ip_lock); | 
 | 1518 | 	oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL; | 
 | 1519 | 	di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features); | 
 | 1520 | 	spin_unlock(&oi->ip_lock); | 
 | 1521 |  | 
 | 1522 | 	ocfs2_dinode_new_extent_list(dir, di); | 
 | 1523 |  | 
 | 1524 | 	i_size_write(dir, sb->s_blocksize); | 
 | 1525 | 	dir->i_mtime = dir->i_ctime = CURRENT_TIME; | 
 | 1526 |  | 
 | 1527 | 	di->i_size = cpu_to_le64(sb->s_blocksize); | 
 | 1528 | 	di->i_ctime = di->i_mtime = cpu_to_le64(dir->i_ctime.tv_sec); | 
 | 1529 | 	di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(dir->i_ctime.tv_nsec); | 
| Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1530 |  | 
 | 1531 | 	/* | 
 | 1532 | 	 * This should never fail as our extent list is empty and all | 
 | 1533 | 	 * related blocks have been journaled already. | 
 | 1534 | 	 */ | 
| Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 1535 | 	ret = ocfs2_insert_extent(osb, handle, dir, &et, 0, blkno, len, | 
 | 1536 | 				  0, NULL); | 
| Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1537 | 	if (ret) { | 
 | 1538 | 		mlog_errno(ret); | 
| Tao Ma | 83cab53 | 2008-08-21 14:14:27 +0800 | [diff] [blame] | 1539 | 		goto out_commit; | 
| Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1540 | 	} | 
 | 1541 |  | 
| Mark Fasheh | 9780eb6 | 2008-08-05 11:32:46 -0700 | [diff] [blame] | 1542 | 	/* | 
 | 1543 | 	 * Set i_blocks after the extent insert for the most up to | 
 | 1544 | 	 * date ip_clusters value. | 
 | 1545 | 	 */ | 
 | 1546 | 	dir->i_blocks = ocfs2_inode_sector_count(dir); | 
 | 1547 |  | 
| Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1548 | 	ret = ocfs2_journal_dirty(handle, di_bh); | 
 | 1549 | 	if (ret) { | 
 | 1550 | 		mlog_errno(ret); | 
 | 1551 | 		goto out_commit; | 
 | 1552 | 	} | 
 | 1553 |  | 
 | 1554 | 	/* | 
 | 1555 | 	 * We asked for two clusters, but only got one in the 1st | 
 | 1556 | 	 * pass. Claim the 2nd cluster as a separate extent. | 
 | 1557 | 	 */ | 
 | 1558 | 	if (alloc > len) { | 
 | 1559 | 		ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off, | 
 | 1560 | 					   &len); | 
 | 1561 | 		if (ret) { | 
 | 1562 | 			mlog_errno(ret); | 
 | 1563 | 			goto out_commit; | 
 | 1564 | 		} | 
 | 1565 | 		blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off); | 
 | 1566 |  | 
| Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 1567 | 		ret = ocfs2_insert_extent(osb, handle, dir, &et, 1, | 
 | 1568 | 					  blkno, len, 0, NULL); | 
| Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1569 | 		if (ret) { | 
 | 1570 | 			mlog_errno(ret); | 
| Tao Ma | 83cab53 | 2008-08-21 14:14:27 +0800 | [diff] [blame] | 1571 | 			goto out_commit; | 
| Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1572 | 		} | 
 | 1573 | 	} | 
 | 1574 |  | 
 | 1575 | 	*first_block_bh = dirdata_bh; | 
 | 1576 | 	dirdata_bh = NULL; | 
 | 1577 |  | 
 | 1578 | out_commit: | 
| Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 1579 | 	if (ret < 0 && did_quota) | 
 | 1580 | 		vfs_dq_free_space_nodirty(dir, | 
 | 1581 | 			ocfs2_clusters_to_bytes(osb->sb, 2)); | 
| Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1582 | 	ocfs2_commit_trans(osb, handle); | 
 | 1583 |  | 
 | 1584 | out_sem: | 
 | 1585 | 	up_write(&oi->ip_alloc_sem); | 
 | 1586 |  | 
 | 1587 | out: | 
 | 1588 | 	if (data_ac) | 
 | 1589 | 		ocfs2_free_alloc_context(data_ac); | 
 | 1590 |  | 
 | 1591 | 	brelse(dirdata_bh); | 
 | 1592 |  | 
 | 1593 | 	return ret; | 
 | 1594 | } | 
 | 1595 |  | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1596 | /* returns a bh of the 1st new block in the allocation. */ | 
| Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 1597 | static int ocfs2_do_extend_dir(struct super_block *sb, | 
 | 1598 | 			       handle_t *handle, | 
 | 1599 | 			       struct inode *dir, | 
 | 1600 | 			       struct buffer_head *parent_fe_bh, | 
 | 1601 | 			       struct ocfs2_alloc_context *data_ac, | 
 | 1602 | 			       struct ocfs2_alloc_context *meta_ac, | 
 | 1603 | 			       struct buffer_head **new_bh) | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1604 | { | 
 | 1605 | 	int status; | 
| Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 1606 | 	int extend, did_quota = 0; | 
| Mark Fasheh | 8110b07 | 2007-03-22 16:53:23 -0700 | [diff] [blame] | 1607 | 	u64 p_blkno, v_blkno; | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1608 |  | 
 | 1609 | 	spin_lock(&OCFS2_I(dir)->ip_lock); | 
 | 1610 | 	extend = (i_size_read(dir) == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)); | 
 | 1611 | 	spin_unlock(&OCFS2_I(dir)->ip_lock); | 
 | 1612 |  | 
 | 1613 | 	if (extend) { | 
| Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1614 | 		u32 offset = OCFS2_I(dir)->ip_clusters; | 
 | 1615 |  | 
| Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 1616 | 		if (vfs_dq_alloc_space_nodirty(dir, | 
 | 1617 | 					ocfs2_clusters_to_bytes(sb, 1))) { | 
 | 1618 | 			status = -EDQUOT; | 
 | 1619 | 			goto bail; | 
 | 1620 | 		} | 
 | 1621 | 		did_quota = 1; | 
 | 1622 |  | 
| Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 1623 | 		status = ocfs2_add_inode_data(OCFS2_SB(sb), dir, &offset, | 
 | 1624 | 					      1, 0, parent_fe_bh, handle, | 
 | 1625 | 					      data_ac, meta_ac, NULL); | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1626 | 		BUG_ON(status == -EAGAIN); | 
 | 1627 | 		if (status < 0) { | 
 | 1628 | 			mlog_errno(status); | 
 | 1629 | 			goto bail; | 
 | 1630 | 		} | 
 | 1631 | 	} | 
 | 1632 |  | 
| Mark Fasheh | 8110b07 | 2007-03-22 16:53:23 -0700 | [diff] [blame] | 1633 | 	v_blkno = ocfs2_blocks_for_bytes(sb, i_size_read(dir)); | 
 | 1634 | 	status = ocfs2_extent_map_get_blocks(dir, v_blkno, &p_blkno, NULL, NULL); | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1635 | 	if (status < 0) { | 
 | 1636 | 		mlog_errno(status); | 
 | 1637 | 		goto bail; | 
 | 1638 | 	} | 
 | 1639 |  | 
 | 1640 | 	*new_bh = sb_getblk(sb, p_blkno); | 
 | 1641 | 	if (!*new_bh) { | 
 | 1642 | 		status = -EIO; | 
 | 1643 | 		mlog_errno(status); | 
 | 1644 | 		goto bail; | 
 | 1645 | 	} | 
 | 1646 | 	status = 0; | 
 | 1647 | bail: | 
| Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 1648 | 	if (did_quota && status < 0) | 
 | 1649 | 		vfs_dq_free_space_nodirty(dir, ocfs2_clusters_to_bytes(sb, 1)); | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1650 | 	mlog_exit(status); | 
 | 1651 | 	return status; | 
 | 1652 | } | 
 | 1653 |  | 
| Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1654 | /* | 
 | 1655 |  * Assumes you already have a cluster lock on the directory. | 
 | 1656 |  * | 
 | 1657 |  * 'blocks_wanted' is only used if we have an inline directory which | 
 | 1658 |  * is to be turned into an extent based one. The size of the dirent to | 
 | 1659 |  * insert might be larger than the space gained by growing to just one | 
 | 1660 |  * block, so we may have to grow the inode by two blocks in that case. | 
 | 1661 |  */ | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1662 | static int ocfs2_extend_dir(struct ocfs2_super *osb, | 
 | 1663 | 			    struct inode *dir, | 
 | 1664 | 			    struct buffer_head *parent_fe_bh, | 
| Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1665 | 			    unsigned int blocks_wanted, | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1666 | 			    struct buffer_head **new_de_bh) | 
 | 1667 | { | 
 | 1668 | 	int status = 0; | 
| Joel Becker | ee19a77 | 2007-03-28 18:27:07 -0700 | [diff] [blame] | 1669 | 	int credits, num_free_extents, drop_alloc_sem = 0; | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1670 | 	loff_t dir_i_size; | 
 | 1671 | 	struct ocfs2_dinode *fe = (struct ocfs2_dinode *) parent_fe_bh->b_data; | 
| Tao Ma | 811f933 | 2008-08-18 17:38:43 +0800 | [diff] [blame] | 1672 | 	struct ocfs2_extent_list *el = &fe->id2.i_list; | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1673 | 	struct ocfs2_alloc_context *data_ac = NULL; | 
 | 1674 | 	struct ocfs2_alloc_context *meta_ac = NULL; | 
| Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 1675 | 	handle_t *handle = NULL; | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1676 | 	struct buffer_head *new_bh = NULL; | 
 | 1677 | 	struct ocfs2_dir_entry * de; | 
 | 1678 | 	struct super_block *sb = osb->sb; | 
| Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 1679 | 	struct ocfs2_extent_tree et; | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1680 |  | 
 | 1681 | 	mlog_entry_void(); | 
 | 1682 |  | 
| Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1683 | 	if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) { | 
 | 1684 | 		status = ocfs2_expand_inline_dir(dir, parent_fe_bh, | 
 | 1685 | 						 blocks_wanted, &new_bh); | 
 | 1686 | 		if (status) { | 
 | 1687 | 			mlog_errno(status); | 
 | 1688 | 			goto bail; | 
 | 1689 | 		} | 
 | 1690 |  | 
 | 1691 | 		if (blocks_wanted == 1) { | 
 | 1692 | 			/* | 
 | 1693 | 			 * If the new dirent will fit inside the space | 
 | 1694 | 			 * created by pushing out to one block, then | 
 | 1695 | 			 * we can complete the operation | 
 | 1696 | 			 * here. Otherwise we have to expand i_size | 
 | 1697 | 			 * and format the 2nd block below. | 
 | 1698 | 			 */ | 
 | 1699 | 			BUG_ON(new_bh == NULL); | 
 | 1700 | 			goto bail_bh; | 
 | 1701 | 		} | 
 | 1702 |  | 
 | 1703 | 		/* | 
 | 1704 | 		 * Get rid of 'new_bh' - we want to format the 2nd | 
 | 1705 | 		 * data block and return that instead. | 
 | 1706 | 		 */ | 
 | 1707 | 		brelse(new_bh); | 
 | 1708 | 		new_bh = NULL; | 
 | 1709 |  | 
 | 1710 | 		dir_i_size = i_size_read(dir); | 
 | 1711 | 		credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS; | 
 | 1712 | 		goto do_extend; | 
 | 1713 | 	} | 
 | 1714 |  | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1715 | 	dir_i_size = i_size_read(dir); | 
| Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 1716 | 	mlog(0, "extending dir %llu (i_size = %lld)\n", | 
 | 1717 | 	     (unsigned long long)OCFS2_I(dir)->ip_blkno, dir_i_size); | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1718 |  | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1719 | 	/* dir->i_size is always block aligned. */ | 
 | 1720 | 	spin_lock(&OCFS2_I(dir)->ip_lock); | 
 | 1721 | 	if (dir_i_size == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)) { | 
 | 1722 | 		spin_unlock(&OCFS2_I(dir)->ip_lock); | 
| Joel Becker | 8d6220d | 2008-08-22 12:46:09 -0700 | [diff] [blame] | 1723 | 		ocfs2_init_dinode_extent_tree(&et, dir, parent_fe_bh); | 
| Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 1724 | 		num_free_extents = ocfs2_num_free_extents(osb, dir, &et); | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1725 | 		if (num_free_extents < 0) { | 
 | 1726 | 			status = num_free_extents; | 
 | 1727 | 			mlog_errno(status); | 
 | 1728 | 			goto bail; | 
 | 1729 | 		} | 
 | 1730 |  | 
 | 1731 | 		if (!num_free_extents) { | 
| Tao Ma | 811f933 | 2008-08-18 17:38:43 +0800 | [diff] [blame] | 1732 | 			status = ocfs2_reserve_new_metadata(osb, el, &meta_ac); | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1733 | 			if (status < 0) { | 
 | 1734 | 				if (status != -ENOSPC) | 
 | 1735 | 					mlog_errno(status); | 
 | 1736 | 				goto bail; | 
 | 1737 | 			} | 
 | 1738 | 		} | 
 | 1739 |  | 
| Mark Fasheh | da5cbf2 | 2006-10-06 18:34:35 -0700 | [diff] [blame] | 1740 | 		status = ocfs2_reserve_clusters(osb, 1, &data_ac); | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1741 | 		if (status < 0) { | 
 | 1742 | 			if (status != -ENOSPC) | 
 | 1743 | 				mlog_errno(status); | 
 | 1744 | 			goto bail; | 
 | 1745 | 		} | 
 | 1746 |  | 
| Tao Ma | 811f933 | 2008-08-18 17:38:43 +0800 | [diff] [blame] | 1747 | 		credits = ocfs2_calc_extend_credits(sb, el, 1); | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1748 | 	} else { | 
 | 1749 | 		spin_unlock(&OCFS2_I(dir)->ip_lock); | 
 | 1750 | 		credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS; | 
 | 1751 | 	} | 
 | 1752 |  | 
| Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1753 | do_extend: | 
| Joel Becker | ee19a77 | 2007-03-28 18:27:07 -0700 | [diff] [blame] | 1754 | 	down_write(&OCFS2_I(dir)->ip_alloc_sem); | 
 | 1755 | 	drop_alloc_sem = 1; | 
 | 1756 |  | 
| Mark Fasheh | 65eff9c | 2006-10-09 17:26:22 -0700 | [diff] [blame] | 1757 | 	handle = ocfs2_start_trans(osb, credits); | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1758 | 	if (IS_ERR(handle)) { | 
 | 1759 | 		status = PTR_ERR(handle); | 
 | 1760 | 		handle = NULL; | 
 | 1761 | 		mlog_errno(status); | 
 | 1762 | 		goto bail; | 
 | 1763 | 	} | 
 | 1764 |  | 
 | 1765 | 	status = ocfs2_do_extend_dir(osb->sb, handle, dir, parent_fe_bh, | 
 | 1766 | 				     data_ac, meta_ac, &new_bh); | 
 | 1767 | 	if (status < 0) { | 
 | 1768 | 		mlog_errno(status); | 
 | 1769 | 		goto bail; | 
 | 1770 | 	} | 
 | 1771 |  | 
 | 1772 | 	ocfs2_set_new_buffer_uptodate(dir, new_bh); | 
 | 1773 |  | 
| Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1774 | 	status = ocfs2_journal_access_db(handle, dir, new_bh, | 
 | 1775 | 					 OCFS2_JOURNAL_ACCESS_CREATE); | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1776 | 	if (status < 0) { | 
 | 1777 | 		mlog_errno(status); | 
 | 1778 | 		goto bail; | 
 | 1779 | 	} | 
 | 1780 | 	memset(new_bh->b_data, 0, sb->s_blocksize); | 
| Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 1781 |  | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1782 | 	de = (struct ocfs2_dir_entry *) new_bh->b_data; | 
 | 1783 | 	de->inode = 0; | 
| Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 1784 | 	if (ocfs2_dir_has_trailer(dir)) { | 
 | 1785 | 		de->rec_len = cpu_to_le16(ocfs2_dir_trailer_blk_off(sb)); | 
 | 1786 | 		ocfs2_init_dir_trailer(dir, new_bh); | 
 | 1787 | 	} else { | 
 | 1788 | 		de->rec_len = cpu_to_le16(sb->s_blocksize); | 
 | 1789 | 	} | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1790 | 	status = ocfs2_journal_dirty(handle, new_bh); | 
 | 1791 | 	if (status < 0) { | 
 | 1792 | 		mlog_errno(status); | 
 | 1793 | 		goto bail; | 
 | 1794 | 	} | 
 | 1795 |  | 
 | 1796 | 	dir_i_size += dir->i_sb->s_blocksize; | 
 | 1797 | 	i_size_write(dir, dir_i_size); | 
| Mark Fasheh | 8110b07 | 2007-03-22 16:53:23 -0700 | [diff] [blame] | 1798 | 	dir->i_blocks = ocfs2_inode_sector_count(dir); | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1799 | 	status = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh); | 
 | 1800 | 	if (status < 0) { | 
 | 1801 | 		mlog_errno(status); | 
 | 1802 | 		goto bail; | 
 | 1803 | 	} | 
 | 1804 |  | 
| Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1805 | bail_bh: | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1806 | 	*new_de_bh = new_bh; | 
 | 1807 | 	get_bh(*new_de_bh); | 
 | 1808 | bail: | 
| Joel Becker | ee19a77 | 2007-03-28 18:27:07 -0700 | [diff] [blame] | 1809 | 	if (drop_alloc_sem) | 
 | 1810 | 		up_write(&OCFS2_I(dir)->ip_alloc_sem); | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1811 | 	if (handle) | 
| Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 1812 | 		ocfs2_commit_trans(osb, handle); | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1813 |  | 
 | 1814 | 	if (data_ac) | 
 | 1815 | 		ocfs2_free_alloc_context(data_ac); | 
 | 1816 | 	if (meta_ac) | 
 | 1817 | 		ocfs2_free_alloc_context(meta_ac); | 
 | 1818 |  | 
| Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 1819 | 	brelse(new_bh); | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1820 |  | 
 | 1821 | 	mlog_exit(status); | 
 | 1822 | 	return status; | 
 | 1823 | } | 
 | 1824 |  | 
| Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1825 | static int ocfs2_find_dir_space_id(struct inode *dir, struct buffer_head *di_bh, | 
 | 1826 | 				   const char *name, int namelen, | 
 | 1827 | 				   struct buffer_head **ret_de_bh, | 
 | 1828 | 				   unsigned int *blocks_wanted) | 
 | 1829 | { | 
 | 1830 | 	int ret; | 
| Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 1831 | 	struct super_block *sb = dir->i_sb; | 
| Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1832 | 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; | 
 | 1833 | 	struct ocfs2_dir_entry *de, *last_de = NULL; | 
 | 1834 | 	char *de_buf, *limit; | 
 | 1835 | 	unsigned long offset = 0; | 
| Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 1836 | 	unsigned int rec_len, new_rec_len, free_space = dir->i_sb->s_blocksize; | 
 | 1837 |  | 
 | 1838 | 	/* | 
 | 1839 | 	 * This calculates how many free bytes we'd have in block zero, should | 
 | 1840 | 	 * this function force expansion to an extent tree. | 
 | 1841 | 	 */ | 
 | 1842 | 	if (ocfs2_supports_dir_trailer(OCFS2_SB(sb))) | 
 | 1843 | 		free_space = ocfs2_dir_trailer_blk_off(sb) - i_size_read(dir); | 
 | 1844 | 	else | 
 | 1845 | 		free_space = dir->i_sb->s_blocksize - i_size_read(dir); | 
| Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1846 |  | 
 | 1847 | 	de_buf = di->id2.i_data.id_data; | 
 | 1848 | 	limit = de_buf + i_size_read(dir); | 
 | 1849 | 	rec_len = OCFS2_DIR_REC_LEN(namelen); | 
 | 1850 |  | 
 | 1851 | 	while (de_buf < limit) { | 
 | 1852 | 		de = (struct ocfs2_dir_entry *)de_buf; | 
 | 1853 |  | 
 | 1854 | 		if (!ocfs2_check_dir_entry(dir, de, di_bh, offset)) { | 
 | 1855 | 			ret = -ENOENT; | 
 | 1856 | 			goto out; | 
 | 1857 | 		} | 
 | 1858 | 		if (ocfs2_match(namelen, name, de)) { | 
 | 1859 | 			ret = -EEXIST; | 
 | 1860 | 			goto out; | 
 | 1861 | 		} | 
| Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 1862 | 		/* | 
 | 1863 | 		 * No need to check for a trailing dirent record here as | 
 | 1864 | 		 * they're not used for inline dirs. | 
 | 1865 | 		 */ | 
 | 1866 |  | 
| Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1867 | 		if (ocfs2_dirent_would_fit(de, rec_len)) { | 
 | 1868 | 			/* Ok, we found a spot. Return this bh and let | 
 | 1869 | 			 * the caller actually fill it in. */ | 
 | 1870 | 			*ret_de_bh = di_bh; | 
 | 1871 | 			get_bh(*ret_de_bh); | 
 | 1872 | 			ret = 0; | 
 | 1873 | 			goto out; | 
 | 1874 | 		} | 
 | 1875 |  | 
 | 1876 | 		last_de = de; | 
 | 1877 | 		de_buf += le16_to_cpu(de->rec_len); | 
 | 1878 | 		offset += le16_to_cpu(de->rec_len); | 
 | 1879 | 	} | 
 | 1880 |  | 
 | 1881 | 	/* | 
 | 1882 | 	 * We're going to require expansion of the directory - figure | 
 | 1883 | 	 * out how many blocks we'll need so that a place for the | 
 | 1884 | 	 * dirent can be found. | 
 | 1885 | 	 */ | 
 | 1886 | 	*blocks_wanted = 1; | 
| Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 1887 | 	new_rec_len = le16_to_cpu(last_de->rec_len) + free_space; | 
| Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1888 | 	if (new_rec_len < (rec_len + OCFS2_DIR_REC_LEN(last_de->name_len))) | 
 | 1889 | 		*blocks_wanted = 2; | 
 | 1890 |  | 
 | 1891 | 	ret = -ENOSPC; | 
 | 1892 | out: | 
 | 1893 | 	return ret; | 
 | 1894 | } | 
 | 1895 |  | 
 | 1896 | static int ocfs2_find_dir_space_el(struct inode *dir, const char *name, | 
 | 1897 | 				   int namelen, struct buffer_head **ret_de_bh) | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1898 | { | 
 | 1899 | 	unsigned long offset; | 
| Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1900 | 	struct buffer_head *bh = NULL; | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1901 | 	unsigned short rec_len; | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1902 | 	struct ocfs2_dir_entry *de; | 
| Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1903 | 	struct super_block *sb = dir->i_sb; | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1904 | 	int status; | 
| Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 1905 | 	int blocksize = dir->i_sb->s_blocksize; | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1906 |  | 
| Joel Becker | a22305c | 2008-11-13 14:49:17 -0800 | [diff] [blame] | 1907 | 	status = ocfs2_read_dir_block(dir, 0, &bh, 0); | 
 | 1908 | 	if (status) { | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1909 | 		mlog_errno(status); | 
 | 1910 | 		goto bail; | 
 | 1911 | 	} | 
 | 1912 |  | 
 | 1913 | 	rec_len = OCFS2_DIR_REC_LEN(namelen); | 
 | 1914 | 	offset = 0; | 
 | 1915 | 	de = (struct ocfs2_dir_entry *) bh->b_data; | 
 | 1916 | 	while (1) { | 
 | 1917 | 		if ((char *)de >= sb->s_blocksize + bh->b_data) { | 
 | 1918 | 			brelse(bh); | 
 | 1919 | 			bh = NULL; | 
 | 1920 |  | 
 | 1921 | 			if (i_size_read(dir) <= offset) { | 
| Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1922 | 				/* | 
 | 1923 | 				 * Caller will have to expand this | 
 | 1924 | 				 * directory. | 
 | 1925 | 				 */ | 
 | 1926 | 				status = -ENOSPC; | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1927 | 				goto bail; | 
 | 1928 | 			} | 
| Joel Becker | a22305c | 2008-11-13 14:49:17 -0800 | [diff] [blame] | 1929 | 			status = ocfs2_read_dir_block(dir, | 
 | 1930 | 					     offset >> sb->s_blocksize_bits, | 
 | 1931 | 					     &bh, 0); | 
 | 1932 | 			if (status) { | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1933 | 				mlog_errno(status); | 
 | 1934 | 				goto bail; | 
 | 1935 | 			} | 
 | 1936 | 			/* move to next block */ | 
 | 1937 | 			de = (struct ocfs2_dir_entry *) bh->b_data; | 
 | 1938 | 		} | 
 | 1939 | 		if (!ocfs2_check_dir_entry(dir, de, bh, offset)) { | 
 | 1940 | 			status = -ENOENT; | 
 | 1941 | 			goto bail; | 
 | 1942 | 		} | 
 | 1943 | 		if (ocfs2_match(namelen, name, de)) { | 
 | 1944 | 			status = -EEXIST; | 
 | 1945 | 			goto bail; | 
 | 1946 | 		} | 
| Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 1947 |  | 
 | 1948 | 		if (ocfs2_skip_dir_trailer(dir, de, offset % blocksize, | 
 | 1949 | 					   blocksize)) | 
 | 1950 | 			goto next; | 
 | 1951 |  | 
| Mark Fasheh | 8553cf4 | 2007-09-13 16:29:01 -0700 | [diff] [blame] | 1952 | 		if (ocfs2_dirent_would_fit(de, rec_len)) { | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1953 | 			/* Ok, we found a spot. Return this bh and let | 
 | 1954 | 			 * the caller actually fill it in. */ | 
 | 1955 | 			*ret_de_bh = bh; | 
 | 1956 | 			get_bh(*ret_de_bh); | 
 | 1957 | 			status = 0; | 
 | 1958 | 			goto bail; | 
 | 1959 | 		} | 
| Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 1960 | next: | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1961 | 		offset += le16_to_cpu(de->rec_len); | 
 | 1962 | 		de = (struct ocfs2_dir_entry *)((char *) de + le16_to_cpu(de->rec_len)); | 
 | 1963 | 	} | 
 | 1964 |  | 
 | 1965 | 	status = 0; | 
 | 1966 | bail: | 
| Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 1967 | 	brelse(bh); | 
| Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1968 |  | 
 | 1969 | 	mlog_exit(status); | 
 | 1970 | 	return status; | 
 | 1971 | } | 
| Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1972 |  | 
 | 1973 | int ocfs2_prepare_dir_for_insert(struct ocfs2_super *osb, | 
 | 1974 | 				 struct inode *dir, | 
 | 1975 | 				 struct buffer_head *parent_fe_bh, | 
 | 1976 | 				 const char *name, | 
 | 1977 | 				 int namelen, | 
 | 1978 | 				 struct buffer_head **ret_de_bh) | 
 | 1979 | { | 
 | 1980 | 	int ret; | 
 | 1981 | 	unsigned int blocks_wanted = 1; | 
 | 1982 | 	struct buffer_head *bh = NULL; | 
 | 1983 |  | 
 | 1984 | 	mlog(0, "getting ready to insert namelen %d into dir %llu\n", | 
 | 1985 | 	     namelen, (unsigned long long)OCFS2_I(dir)->ip_blkno); | 
 | 1986 |  | 
 | 1987 | 	*ret_de_bh = NULL; | 
 | 1988 |  | 
 | 1989 | 	if (!namelen) { | 
 | 1990 | 		ret = -EINVAL; | 
 | 1991 | 		mlog_errno(ret); | 
 | 1992 | 		goto out; | 
 | 1993 | 	} | 
 | 1994 |  | 
 | 1995 | 	if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) { | 
 | 1996 | 		ret = ocfs2_find_dir_space_id(dir, parent_fe_bh, name, | 
 | 1997 | 					      namelen, &bh, &blocks_wanted); | 
 | 1998 | 	} else | 
 | 1999 | 		ret = ocfs2_find_dir_space_el(dir, name, namelen, &bh); | 
 | 2000 |  | 
 | 2001 | 	if (ret && ret != -ENOSPC) { | 
 | 2002 | 		mlog_errno(ret); | 
 | 2003 | 		goto out; | 
 | 2004 | 	} | 
 | 2005 |  | 
 | 2006 | 	if (ret == -ENOSPC) { | 
 | 2007 | 		/* | 
 | 2008 | 		 * We have to expand the directory to add this name. | 
 | 2009 | 		 */ | 
 | 2010 | 		BUG_ON(bh); | 
 | 2011 |  | 
 | 2012 | 		ret = ocfs2_extend_dir(osb, dir, parent_fe_bh, blocks_wanted, | 
 | 2013 | 				       &bh); | 
 | 2014 | 		if (ret) { | 
 | 2015 | 			if (ret != -ENOSPC) | 
 | 2016 | 				mlog_errno(ret); | 
 | 2017 | 			goto out; | 
 | 2018 | 		} | 
 | 2019 |  | 
 | 2020 | 		BUG_ON(!bh); | 
 | 2021 | 	} | 
 | 2022 |  | 
 | 2023 | 	*ret_de_bh = bh; | 
 | 2024 | 	bh = NULL; | 
 | 2025 | out: | 
| Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 2026 | 	brelse(bh); | 
| Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 2027 | 	return ret; | 
 | 2028 | } |