11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/fs/ufs/ufs_dir.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 1996 51da177e4SLinus Torvalds * Adrian Rodriguez (adrian@franklins-tower.rutgers.edu) 61da177e4SLinus Torvalds * Laboratory for Computer Science Research Computing Facility 71da177e4SLinus Torvalds * Rutgers, The State University of New Jersey 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * swab support by Francois-Rene Rideau <fare@tunes.org> 19970406 101da177e4SLinus Torvalds * 111da177e4SLinus Torvalds * 4.4BSD (FreeBSD) support added on February 1st 1998 by 121da177e4SLinus Torvalds * Niels Kristian Bech Jensen <nkbj@image.dk> partially based 131da177e4SLinus Torvalds * on code by Martin von Loewis <martin@mira.isdn.cs.tu-berlin.de>. 14b71034e5SEvgeniy Dushistov * 15b71034e5SEvgeniy Dushistov * Migration to usage of "page cache" on May 2006 by 16b71034e5SEvgeniy Dushistov * Evgeniy Dushistov <dushistov@mail.ru> based on ext2 code base. 171da177e4SLinus Torvalds */ 181da177e4SLinus Torvalds 191da177e4SLinus Torvalds #include <linux/time.h> 201da177e4SLinus Torvalds #include <linux/fs.h> 2182b9d1d0SNick Piggin #include <linux/swap.h> 221da177e4SLinus Torvalds 23e5420598SMike Frysinger #include "ufs_fs.h" 24bcd6d4ecSChristoph Hellwig #include "ufs.h" 251da177e4SLinus Torvalds #include "swab.h" 261da177e4SLinus Torvalds #include "util.h" 271da177e4SLinus Torvalds 281da177e4SLinus Torvalds /* 291da177e4SLinus Torvalds * NOTE! unlike strncmp, ufs_match returns 1 for success, 0 for failure. 301da177e4SLinus Torvalds * 311da177e4SLinus Torvalds * len <= UFS_MAXNAMLEN and de != NULL are guaranteed by caller. 321da177e4SLinus Torvalds */ 331da177e4SLinus Torvalds static inline int ufs_match(struct super_block *sb, int len, 3489031bc7SAl Viro const unsigned char *name, struct ufs_dir_entry *de) 351da177e4SLinus Torvalds { 361da177e4SLinus Torvalds if (len != ufs_get_de_namlen(sb, de)) 371da177e4SLinus Torvalds return 0; 381da177e4SLinus Torvalds if (!de->d_ino) 391da177e4SLinus Torvalds return 0; 401da177e4SLinus Torvalds return !memcmp(name, de->d_name, len); 411da177e4SLinus Torvalds } 421da177e4SLinus Torvalds 4382b9d1d0SNick Piggin static int ufs_commit_chunk(struct page *page, loff_t pos, unsigned len) 44b71034e5SEvgeniy Dushistov { 4582b9d1d0SNick Piggin struct address_space *mapping = page->mapping; 4682b9d1d0SNick Piggin struct inode *dir = mapping->host; 47b71034e5SEvgeniy Dushistov int err = 0; 4882b9d1d0SNick Piggin 49b71034e5SEvgeniy Dushistov dir->i_version++; 5082b9d1d0SNick Piggin block_write_end(NULL, mapping, pos, len, len, page, NULL); 5182b9d1d0SNick Piggin if (pos+len > dir->i_size) { 5282b9d1d0SNick Piggin i_size_write(dir, pos+len); 5382b9d1d0SNick Piggin mark_inode_dirty(dir); 5482b9d1d0SNick Piggin } 55b71034e5SEvgeniy Dushistov if (IS_DIRSYNC(dir)) 56b71034e5SEvgeniy Dushistov err = write_one_page(page, 1); 57b71034e5SEvgeniy Dushistov else 58b71034e5SEvgeniy Dushistov unlock_page(page); 59b71034e5SEvgeniy Dushistov return err; 60b71034e5SEvgeniy Dushistov } 61b71034e5SEvgeniy Dushistov 62b71034e5SEvgeniy Dushistov static inline void ufs_put_page(struct page *page) 63b71034e5SEvgeniy Dushistov { 64b71034e5SEvgeniy Dushistov kunmap(page); 6509cbfeafSKirill A. Shutemov put_page(page); 66b71034e5SEvgeniy Dushistov } 67b71034e5SEvgeniy Dushistov 6889031bc7SAl Viro ino_t ufs_inode_by_name(struct inode *dir, const struct qstr *qstr) 69b71034e5SEvgeniy Dushistov { 70b71034e5SEvgeniy Dushistov ino_t res = 0; 71b71034e5SEvgeniy Dushistov struct ufs_dir_entry *de; 72b71034e5SEvgeniy Dushistov struct page *page; 73b71034e5SEvgeniy Dushistov 7408049707SAlexey Dobriyan de = ufs_find_entry(dir, qstr, &page); 75b71034e5SEvgeniy Dushistov if (de) { 76b71034e5SEvgeniy Dushistov res = fs32_to_cpu(dir->i_sb, de->d_ino); 77b71034e5SEvgeniy Dushistov ufs_put_page(page); 78b71034e5SEvgeniy Dushistov } 79b71034e5SEvgeniy Dushistov return res; 80b71034e5SEvgeniy Dushistov } 81b71034e5SEvgeniy Dushistov 82b71034e5SEvgeniy Dushistov 83b71034e5SEvgeniy Dushistov /* Releases the page */ 84b71034e5SEvgeniy Dushistov void ufs_set_link(struct inode *dir, struct ufs_dir_entry *de, 8570d45cdbSAl Viro struct page *page, struct inode *inode, 8670d45cdbSAl Viro bool update_times) 87b71034e5SEvgeniy Dushistov { 8882b9d1d0SNick Piggin loff_t pos = page_offset(page) + 8982b9d1d0SNick Piggin (char *) de - (char *) page_address(page); 9082b9d1d0SNick Piggin unsigned len = fs16_to_cpu(dir->i_sb, de->d_reclen); 91b71034e5SEvgeniy Dushistov int err; 92b71034e5SEvgeniy Dushistov 93b71034e5SEvgeniy Dushistov lock_page(page); 94f4e420dcSChristoph Hellwig err = ufs_prepare_chunk(page, pos, len); 95b71034e5SEvgeniy Dushistov BUG_ON(err); 9682b9d1d0SNick Piggin 97b71034e5SEvgeniy Dushistov de->d_ino = cpu_to_fs32(dir->i_sb, inode->i_ino); 98b71034e5SEvgeniy Dushistov ufs_set_de_type(dir->i_sb, de, inode->i_mode); 9982b9d1d0SNick Piggin 10082b9d1d0SNick Piggin err = ufs_commit_chunk(page, pos, len); 101b71034e5SEvgeniy Dushistov ufs_put_page(page); 10270d45cdbSAl Viro if (update_times) 103b71034e5SEvgeniy Dushistov dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC; 104b71034e5SEvgeniy Dushistov mark_inode_dirty(dir); 105b71034e5SEvgeniy Dushistov } 106b71034e5SEvgeniy Dushistov 107b71034e5SEvgeniy Dushistov 108be5b82dbSAl Viro static bool ufs_check_page(struct page *page) 109b71034e5SEvgeniy Dushistov { 110b71034e5SEvgeniy Dushistov struct inode *dir = page->mapping->host; 111b71034e5SEvgeniy Dushistov struct super_block *sb = dir->i_sb; 112b71034e5SEvgeniy Dushistov char *kaddr = page_address(page); 113b71034e5SEvgeniy Dushistov unsigned offs, rec_len; 11409cbfeafSKirill A. Shutemov unsigned limit = PAGE_SIZE; 115f336953bSEvgeniy Dushistov const unsigned chunk_mask = UFS_SB(sb)->s_uspi->s_dirblksize - 1; 116b71034e5SEvgeniy Dushistov struct ufs_dir_entry *p; 117b71034e5SEvgeniy Dushistov char *error; 118b71034e5SEvgeniy Dushistov 11909cbfeafSKirill A. Shutemov if ((dir->i_size >> PAGE_SHIFT) == page->index) { 12009cbfeafSKirill A. Shutemov limit = dir->i_size & ~PAGE_MASK; 121f336953bSEvgeniy Dushistov if (limit & chunk_mask) 122b71034e5SEvgeniy Dushistov goto Ebadsize; 123b71034e5SEvgeniy Dushistov if (!limit) 124b71034e5SEvgeniy Dushistov goto out; 125b71034e5SEvgeniy Dushistov } 126b71034e5SEvgeniy Dushistov for (offs = 0; offs <= limit - UFS_DIR_REC_LEN(1); offs += rec_len) { 127b71034e5SEvgeniy Dushistov p = (struct ufs_dir_entry *)(kaddr + offs); 128b71034e5SEvgeniy Dushistov rec_len = fs16_to_cpu(sb, p->d_reclen); 129b71034e5SEvgeniy Dushistov 130b71034e5SEvgeniy Dushistov if (rec_len < UFS_DIR_REC_LEN(1)) 131b71034e5SEvgeniy Dushistov goto Eshort; 132b71034e5SEvgeniy Dushistov if (rec_len & 3) 133b71034e5SEvgeniy Dushistov goto Ealign; 134b71034e5SEvgeniy Dushistov if (rec_len < UFS_DIR_REC_LEN(ufs_get_de_namlen(sb, p))) 135b71034e5SEvgeniy Dushistov goto Enamelen; 136f336953bSEvgeniy Dushistov if (((offs + rec_len - 1) ^ offs) & ~chunk_mask) 137b71034e5SEvgeniy Dushistov goto Espan; 138b71034e5SEvgeniy Dushistov if (fs32_to_cpu(sb, p->d_ino) > (UFS_SB(sb)->s_uspi->s_ipg * 139b71034e5SEvgeniy Dushistov UFS_SB(sb)->s_uspi->s_ncg)) 140b71034e5SEvgeniy Dushistov goto Einumber; 141b71034e5SEvgeniy Dushistov } 142b71034e5SEvgeniy Dushistov if (offs != limit) 143b71034e5SEvgeniy Dushistov goto Eend; 144b71034e5SEvgeniy Dushistov out: 145b71034e5SEvgeniy Dushistov SetPageChecked(page); 146be5b82dbSAl Viro return true; 147b71034e5SEvgeniy Dushistov 148b71034e5SEvgeniy Dushistov /* Too bad, we had an error */ 149b71034e5SEvgeniy Dushistov 150b71034e5SEvgeniy Dushistov Ebadsize: 151b71034e5SEvgeniy Dushistov ufs_error(sb, "ufs_check_page", 152b71034e5SEvgeniy Dushistov "size of directory #%lu is not a multiple of chunk size", 153b71034e5SEvgeniy Dushistov dir->i_ino 154b71034e5SEvgeniy Dushistov ); 155b71034e5SEvgeniy Dushistov goto fail; 156b71034e5SEvgeniy Dushistov Eshort: 157b71034e5SEvgeniy Dushistov error = "rec_len is smaller than minimal"; 158b71034e5SEvgeniy Dushistov goto bad_entry; 159b71034e5SEvgeniy Dushistov Ealign: 160b71034e5SEvgeniy Dushistov error = "unaligned directory entry"; 161b71034e5SEvgeniy Dushistov goto bad_entry; 162b71034e5SEvgeniy Dushistov Enamelen: 163b71034e5SEvgeniy Dushistov error = "rec_len is too small for name_len"; 164b71034e5SEvgeniy Dushistov goto bad_entry; 165b71034e5SEvgeniy Dushistov Espan: 166b71034e5SEvgeniy Dushistov error = "directory entry across blocks"; 167b71034e5SEvgeniy Dushistov goto bad_entry; 168b71034e5SEvgeniy Dushistov Einumber: 169b71034e5SEvgeniy Dushistov error = "inode out of bounds"; 170b71034e5SEvgeniy Dushistov bad_entry: 171b71034e5SEvgeniy Dushistov ufs_error (sb, "ufs_check_page", "bad entry in directory #%lu: %s - " 172b71034e5SEvgeniy Dushistov "offset=%lu, rec_len=%d, name_len=%d", 17309cbfeafSKirill A. Shutemov dir->i_ino, error, (page->index<<PAGE_SHIFT)+offs, 174b71034e5SEvgeniy Dushistov rec_len, ufs_get_de_namlen(sb, p)); 175b71034e5SEvgeniy Dushistov goto fail; 176b71034e5SEvgeniy Dushistov Eend: 177b71034e5SEvgeniy Dushistov p = (struct ufs_dir_entry *)(kaddr + offs); 1789746077aSHarvey Harrison ufs_error(sb, __func__, 179b71034e5SEvgeniy Dushistov "entry in directory #%lu spans the page boundary" 180b71034e5SEvgeniy Dushistov "offset=%lu", 18109cbfeafSKirill A. Shutemov dir->i_ino, (page->index<<PAGE_SHIFT)+offs); 182b71034e5SEvgeniy Dushistov fail: 183b71034e5SEvgeniy Dushistov SetPageError(page); 184be5b82dbSAl Viro return false; 185b71034e5SEvgeniy Dushistov } 186b71034e5SEvgeniy Dushistov 187b71034e5SEvgeniy Dushistov static struct page *ufs_get_page(struct inode *dir, unsigned long n) 188b71034e5SEvgeniy Dushistov { 189b71034e5SEvgeniy Dushistov struct address_space *mapping = dir->i_mapping; 1906fe6900eSNick Piggin struct page *page = read_mapping_page(mapping, n, NULL); 191b71034e5SEvgeniy Dushistov if (!IS_ERR(page)) { 192b71034e5SEvgeniy Dushistov kmap(page); 193be5b82dbSAl Viro if (unlikely(!PageChecked(page))) { 194be5b82dbSAl Viro if (PageError(page) || !ufs_check_page(page)) 195b71034e5SEvgeniy Dushistov goto fail; 196b71034e5SEvgeniy Dushistov } 197be5b82dbSAl Viro } 198b71034e5SEvgeniy Dushistov return page; 199b71034e5SEvgeniy Dushistov 200b71034e5SEvgeniy Dushistov fail: 201b71034e5SEvgeniy Dushistov ufs_put_page(page); 202b71034e5SEvgeniy Dushistov return ERR_PTR(-EIO); 203b71034e5SEvgeniy Dushistov } 204b71034e5SEvgeniy Dushistov 205b71034e5SEvgeniy Dushistov /* 206b71034e5SEvgeniy Dushistov * Return the offset into page `page_nr' of the last valid 207b71034e5SEvgeniy Dushistov * byte in that page, plus one. 208b71034e5SEvgeniy Dushistov */ 209b71034e5SEvgeniy Dushistov static unsigned 210b71034e5SEvgeniy Dushistov ufs_last_byte(struct inode *inode, unsigned long page_nr) 211b71034e5SEvgeniy Dushistov { 212b71034e5SEvgeniy Dushistov unsigned last_byte = inode->i_size; 213b71034e5SEvgeniy Dushistov 21409cbfeafSKirill A. Shutemov last_byte -= page_nr << PAGE_SHIFT; 21509cbfeafSKirill A. Shutemov if (last_byte > PAGE_SIZE) 21609cbfeafSKirill A. Shutemov last_byte = PAGE_SIZE; 217b71034e5SEvgeniy Dushistov return last_byte; 218b71034e5SEvgeniy Dushistov } 219b71034e5SEvgeniy Dushistov 220b71034e5SEvgeniy Dushistov static inline struct ufs_dir_entry * 221b71034e5SEvgeniy Dushistov ufs_next_entry(struct super_block *sb, struct ufs_dir_entry *p) 222b71034e5SEvgeniy Dushistov { 223b71034e5SEvgeniy Dushistov return (struct ufs_dir_entry *)((char *)p + 224b71034e5SEvgeniy Dushistov fs16_to_cpu(sb, p->d_reclen)); 225b71034e5SEvgeniy Dushistov } 226b71034e5SEvgeniy Dushistov 227b71034e5SEvgeniy Dushistov struct ufs_dir_entry *ufs_dotdot(struct inode *dir, struct page **p) 228b71034e5SEvgeniy Dushistov { 229b71034e5SEvgeniy Dushistov struct page *page = ufs_get_page(dir, 0); 230b71034e5SEvgeniy Dushistov struct ufs_dir_entry *de = NULL; 231b71034e5SEvgeniy Dushistov 232b71034e5SEvgeniy Dushistov if (!IS_ERR(page)) { 233b71034e5SEvgeniy Dushistov de = ufs_next_entry(dir->i_sb, 234b71034e5SEvgeniy Dushistov (struct ufs_dir_entry *)page_address(page)); 235b71034e5SEvgeniy Dushistov *p = page; 236b71034e5SEvgeniy Dushistov } 237b71034e5SEvgeniy Dushistov return de; 238b71034e5SEvgeniy Dushistov } 239b71034e5SEvgeniy Dushistov 240b71034e5SEvgeniy Dushistov /* 241b71034e5SEvgeniy Dushistov * ufs_find_entry() 242b71034e5SEvgeniy Dushistov * 243b71034e5SEvgeniy Dushistov * finds an entry in the specified directory with the wanted name. It 244b71034e5SEvgeniy Dushistov * returns the page in which the entry was found, and the entry itself 245b71034e5SEvgeniy Dushistov * (as a parameter - res_dir). Page is returned mapped and unlocked. 246b71034e5SEvgeniy Dushistov * Entry is guaranteed to be valid. 247b71034e5SEvgeniy Dushistov */ 24889031bc7SAl Viro struct ufs_dir_entry *ufs_find_entry(struct inode *dir, const struct qstr *qstr, 249b71034e5SEvgeniy Dushistov struct page **res_page) 250b71034e5SEvgeniy Dushistov { 251b71034e5SEvgeniy Dushistov struct super_block *sb = dir->i_sb; 25289031bc7SAl Viro const unsigned char *name = qstr->name; 25308049707SAlexey Dobriyan int namelen = qstr->len; 254b71034e5SEvgeniy Dushistov unsigned reclen = UFS_DIR_REC_LEN(namelen); 255b71034e5SEvgeniy Dushistov unsigned long start, n; 2565d754cedSFabian Frederick unsigned long npages = dir_pages(dir); 257b71034e5SEvgeniy Dushistov struct page *page = NULL; 258dd187a26SEvgeniy Dushistov struct ufs_inode_info *ui = UFS_I(dir); 259b71034e5SEvgeniy Dushistov struct ufs_dir_entry *de; 260b71034e5SEvgeniy Dushistov 261abf5d15fSEvgeniy Dushistov UFSD("ENTER, dir_ino %lu, name %s, namlen %u\n", dir->i_ino, name, namelen); 262b71034e5SEvgeniy Dushistov 263b71034e5SEvgeniy Dushistov if (npages == 0 || namelen > UFS_MAXNAMLEN) 264b71034e5SEvgeniy Dushistov goto out; 265b71034e5SEvgeniy Dushistov 266b71034e5SEvgeniy Dushistov /* OFFSET_CACHE */ 267b71034e5SEvgeniy Dushistov *res_page = NULL; 268b71034e5SEvgeniy Dushistov 269dd187a26SEvgeniy Dushistov start = ui->i_dir_start_lookup; 270dd187a26SEvgeniy Dushistov 271b71034e5SEvgeniy Dushistov if (start >= npages) 272b71034e5SEvgeniy Dushistov start = 0; 273b71034e5SEvgeniy Dushistov n = start; 274b71034e5SEvgeniy Dushistov do { 275b71034e5SEvgeniy Dushistov char *kaddr; 276b71034e5SEvgeniy Dushistov page = ufs_get_page(dir, n); 277b71034e5SEvgeniy Dushistov if (!IS_ERR(page)) { 278b71034e5SEvgeniy Dushistov kaddr = page_address(page); 279b71034e5SEvgeniy Dushistov de = (struct ufs_dir_entry *) kaddr; 280b71034e5SEvgeniy Dushistov kaddr += ufs_last_byte(dir, n) - reclen; 281b71034e5SEvgeniy Dushistov while ((char *) de <= kaddr) { 282b71034e5SEvgeniy Dushistov if (de->d_reclen == 0) { 2839746077aSHarvey Harrison ufs_error(dir->i_sb, __func__, 284b71034e5SEvgeniy Dushistov "zero-length directory entry"); 285b71034e5SEvgeniy Dushistov ufs_put_page(page); 286b71034e5SEvgeniy Dushistov goto out; 287b71034e5SEvgeniy Dushistov } 288b71034e5SEvgeniy Dushistov if (ufs_match(sb, namelen, name, de)) 289b71034e5SEvgeniy Dushistov goto found; 290b71034e5SEvgeniy Dushistov de = ufs_next_entry(sb, de); 291b71034e5SEvgeniy Dushistov } 292b71034e5SEvgeniy Dushistov ufs_put_page(page); 293b71034e5SEvgeniy Dushistov } 294b71034e5SEvgeniy Dushistov if (++n >= npages) 295b71034e5SEvgeniy Dushistov n = 0; 296b71034e5SEvgeniy Dushistov } while (n != start); 297b71034e5SEvgeniy Dushistov out: 298b71034e5SEvgeniy Dushistov return NULL; 299b71034e5SEvgeniy Dushistov 300b71034e5SEvgeniy Dushistov found: 301b71034e5SEvgeniy Dushistov *res_page = page; 302dd187a26SEvgeniy Dushistov ui->i_dir_start_lookup = n; 303b71034e5SEvgeniy Dushistov return de; 304b71034e5SEvgeniy Dushistov } 305b71034e5SEvgeniy Dushistov 306b71034e5SEvgeniy Dushistov /* 307b71034e5SEvgeniy Dushistov * Parent is locked. 308b71034e5SEvgeniy Dushistov */ 309b71034e5SEvgeniy Dushistov int ufs_add_link(struct dentry *dentry, struct inode *inode) 310b71034e5SEvgeniy Dushistov { 3112b0143b5SDavid Howells struct inode *dir = d_inode(dentry->d_parent); 31289031bc7SAl Viro const unsigned char *name = dentry->d_name.name; 313b71034e5SEvgeniy Dushistov int namelen = dentry->d_name.len; 314b71034e5SEvgeniy Dushistov struct super_block *sb = dir->i_sb; 315b71034e5SEvgeniy Dushistov unsigned reclen = UFS_DIR_REC_LEN(namelen); 316f336953bSEvgeniy Dushistov const unsigned int chunk_size = UFS_SB(sb)->s_uspi->s_dirblksize; 317b71034e5SEvgeniy Dushistov unsigned short rec_len, name_len; 318b71034e5SEvgeniy Dushistov struct page *page = NULL; 319b71034e5SEvgeniy Dushistov struct ufs_dir_entry *de; 3205d754cedSFabian Frederick unsigned long npages = dir_pages(dir); 321b71034e5SEvgeniy Dushistov unsigned long n; 322b71034e5SEvgeniy Dushistov char *kaddr; 32382b9d1d0SNick Piggin loff_t pos; 324b71034e5SEvgeniy Dushistov int err; 325b71034e5SEvgeniy Dushistov 326abf5d15fSEvgeniy Dushistov UFSD("ENTER, name %s, namelen %u\n", name, namelen); 327b71034e5SEvgeniy Dushistov 328b71034e5SEvgeniy Dushistov /* 329b71034e5SEvgeniy Dushistov * We take care of directory expansion in the same loop. 330b71034e5SEvgeniy Dushistov * This code plays outside i_size, so it locks the page 331b71034e5SEvgeniy Dushistov * to protect that region. 332b71034e5SEvgeniy Dushistov */ 333b71034e5SEvgeniy Dushistov for (n = 0; n <= npages; n++) { 334b71034e5SEvgeniy Dushistov char *dir_end; 335b71034e5SEvgeniy Dushistov 336b71034e5SEvgeniy Dushistov page = ufs_get_page(dir, n); 337b71034e5SEvgeniy Dushistov err = PTR_ERR(page); 338b71034e5SEvgeniy Dushistov if (IS_ERR(page)) 339b71034e5SEvgeniy Dushistov goto out; 340b71034e5SEvgeniy Dushistov lock_page(page); 341b71034e5SEvgeniy Dushistov kaddr = page_address(page); 342b71034e5SEvgeniy Dushistov dir_end = kaddr + ufs_last_byte(dir, n); 343b71034e5SEvgeniy Dushistov de = (struct ufs_dir_entry *)kaddr; 34409cbfeafSKirill A. Shutemov kaddr += PAGE_SIZE - reclen; 345b71034e5SEvgeniy Dushistov while ((char *)de <= kaddr) { 346b71034e5SEvgeniy Dushistov if ((char *)de == dir_end) { 347b71034e5SEvgeniy Dushistov /* We hit i_size */ 348b71034e5SEvgeniy Dushistov name_len = 0; 349f336953bSEvgeniy Dushistov rec_len = chunk_size; 350f336953bSEvgeniy Dushistov de->d_reclen = cpu_to_fs16(sb, chunk_size); 351b71034e5SEvgeniy Dushistov de->d_ino = 0; 352b71034e5SEvgeniy Dushistov goto got_it; 353b71034e5SEvgeniy Dushistov } 354b71034e5SEvgeniy Dushistov if (de->d_reclen == 0) { 3559746077aSHarvey Harrison ufs_error(dir->i_sb, __func__, 356b71034e5SEvgeniy Dushistov "zero-length directory entry"); 357b71034e5SEvgeniy Dushistov err = -EIO; 358b71034e5SEvgeniy Dushistov goto out_unlock; 359b71034e5SEvgeniy Dushistov } 360b71034e5SEvgeniy Dushistov err = -EEXIST; 361b71034e5SEvgeniy Dushistov if (ufs_match(sb, namelen, name, de)) 362b71034e5SEvgeniy Dushistov goto out_unlock; 363b71034e5SEvgeniy Dushistov name_len = UFS_DIR_REC_LEN(ufs_get_de_namlen(sb, de)); 364b71034e5SEvgeniy Dushistov rec_len = fs16_to_cpu(sb, de->d_reclen); 365b71034e5SEvgeniy Dushistov if (!de->d_ino && rec_len >= reclen) 366b71034e5SEvgeniy Dushistov goto got_it; 367b71034e5SEvgeniy Dushistov if (rec_len >= name_len + reclen) 368b71034e5SEvgeniy Dushistov goto got_it; 369b71034e5SEvgeniy Dushistov de = (struct ufs_dir_entry *) ((char *) de + rec_len); 370b71034e5SEvgeniy Dushistov } 371b71034e5SEvgeniy Dushistov unlock_page(page); 372b71034e5SEvgeniy Dushistov ufs_put_page(page); 373b71034e5SEvgeniy Dushistov } 374b71034e5SEvgeniy Dushistov BUG(); 375b71034e5SEvgeniy Dushistov return -EINVAL; 376b71034e5SEvgeniy Dushistov 377b71034e5SEvgeniy Dushistov got_it: 37882b9d1d0SNick Piggin pos = page_offset(page) + 37982b9d1d0SNick Piggin (char*)de - (char*)page_address(page); 380f4e420dcSChristoph Hellwig err = ufs_prepare_chunk(page, pos, rec_len); 381b71034e5SEvgeniy Dushistov if (err) 382b71034e5SEvgeniy Dushistov goto out_unlock; 383b71034e5SEvgeniy Dushistov if (de->d_ino) { 384b71034e5SEvgeniy Dushistov struct ufs_dir_entry *de1 = 385b71034e5SEvgeniy Dushistov (struct ufs_dir_entry *) ((char *) de + name_len); 386b71034e5SEvgeniy Dushistov de1->d_reclen = cpu_to_fs16(sb, rec_len - name_len); 387b71034e5SEvgeniy Dushistov de->d_reclen = cpu_to_fs16(sb, name_len); 388b71034e5SEvgeniy Dushistov 389b71034e5SEvgeniy Dushistov de = de1; 390b71034e5SEvgeniy Dushistov } 391b71034e5SEvgeniy Dushistov 392b71034e5SEvgeniy Dushistov ufs_set_de_namlen(sb, de, namelen); 393b71034e5SEvgeniy Dushistov memcpy(de->d_name, name, namelen + 1); 394b71034e5SEvgeniy Dushistov de->d_ino = cpu_to_fs32(sb, inode->i_ino); 395b71034e5SEvgeniy Dushistov ufs_set_de_type(sb, de, inode->i_mode); 396b71034e5SEvgeniy Dushistov 39782b9d1d0SNick Piggin err = ufs_commit_chunk(page, pos, rec_len); 398b71034e5SEvgeniy Dushistov dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC; 399b71034e5SEvgeniy Dushistov 400b71034e5SEvgeniy Dushistov mark_inode_dirty(dir); 401b71034e5SEvgeniy Dushistov /* OFFSET_CACHE */ 402b71034e5SEvgeniy Dushistov out_put: 403b71034e5SEvgeniy Dushistov ufs_put_page(page); 404b71034e5SEvgeniy Dushistov out: 405b71034e5SEvgeniy Dushistov return err; 406b71034e5SEvgeniy Dushistov out_unlock: 407b71034e5SEvgeniy Dushistov unlock_page(page); 408b71034e5SEvgeniy Dushistov goto out_put; 409b71034e5SEvgeniy Dushistov } 410b71034e5SEvgeniy Dushistov 411b71034e5SEvgeniy Dushistov static inline unsigned 412b71034e5SEvgeniy Dushistov ufs_validate_entry(struct super_block *sb, char *base, 413b71034e5SEvgeniy Dushistov unsigned offset, unsigned mask) 414b71034e5SEvgeniy Dushistov { 415b71034e5SEvgeniy Dushistov struct ufs_dir_entry *de = (struct ufs_dir_entry*)(base + offset); 416b71034e5SEvgeniy Dushistov struct ufs_dir_entry *p = (struct ufs_dir_entry*)(base + (offset&mask)); 417b71034e5SEvgeniy Dushistov while ((char*)p < (char*)de) { 418b71034e5SEvgeniy Dushistov if (p->d_reclen == 0) 419b71034e5SEvgeniy Dushistov break; 420b71034e5SEvgeniy Dushistov p = ufs_next_entry(sb, p); 421b71034e5SEvgeniy Dushistov } 422b71034e5SEvgeniy Dushistov return (char *)p - base; 423b71034e5SEvgeniy Dushistov } 424b71034e5SEvgeniy Dushistov 425b71034e5SEvgeniy Dushistov 4261da177e4SLinus Torvalds /* 4271da177e4SLinus Torvalds * This is blatantly stolen from ext2fs 4281da177e4SLinus Torvalds */ 4291da177e4SLinus Torvalds static int 43080886298SAl Viro ufs_readdir(struct file *file, struct dir_context *ctx) 4311da177e4SLinus Torvalds { 43280886298SAl Viro loff_t pos = ctx->pos; 43380886298SAl Viro struct inode *inode = file_inode(file); 434b71034e5SEvgeniy Dushistov struct super_block *sb = inode->i_sb; 43509cbfeafSKirill A. Shutemov unsigned int offset = pos & ~PAGE_MASK; 43609cbfeafSKirill A. Shutemov unsigned long n = pos >> PAGE_SHIFT; 4375d754cedSFabian Frederick unsigned long npages = dir_pages(inode); 438f336953bSEvgeniy Dushistov unsigned chunk_mask = ~(UFS_SB(sb)->s_uspi->s_dirblksize - 1); 43980886298SAl Viro int need_revalidate = file->f_version != inode->i_version; 440b71034e5SEvgeniy Dushistov unsigned flags = UFS_SB(sb)->s_flags; 441b71034e5SEvgeniy Dushistov 442abf5d15fSEvgeniy Dushistov UFSD("BEGIN\n"); 443b71034e5SEvgeniy Dushistov 444b71034e5SEvgeniy Dushistov if (pos > inode->i_size - UFS_DIR_REC_LEN(1)) 445b71034e5SEvgeniy Dushistov return 0; 446b71034e5SEvgeniy Dushistov 447b71034e5SEvgeniy Dushistov for ( ; n < npages; n++, offset = 0) { 448b71034e5SEvgeniy Dushistov char *kaddr, *limit; 4491da177e4SLinus Torvalds struct ufs_dir_entry *de; 4501da177e4SLinus Torvalds 451b71034e5SEvgeniy Dushistov struct page *page = ufs_get_page(inode, n); 4521da177e4SLinus Torvalds 453b71034e5SEvgeniy Dushistov if (IS_ERR(page)) { 4549746077aSHarvey Harrison ufs_error(sb, __func__, 455b71034e5SEvgeniy Dushistov "bad page in #%lu", 456b71034e5SEvgeniy Dushistov inode->i_ino); 45709cbfeafSKirill A. Shutemov ctx->pos += PAGE_SIZE - offset; 458b71034e5SEvgeniy Dushistov return -EIO; 4591da177e4SLinus Torvalds } 460b71034e5SEvgeniy Dushistov kaddr = page_address(page); 461b71034e5SEvgeniy Dushistov if (unlikely(need_revalidate)) { 462b71034e5SEvgeniy Dushistov if (offset) { 463b71034e5SEvgeniy Dushistov offset = ufs_validate_entry(sb, kaddr, offset, chunk_mask); 46409cbfeafSKirill A. Shutemov ctx->pos = (n<<PAGE_SHIFT) + offset; 4651da177e4SLinus Torvalds } 46680886298SAl Viro file->f_version = inode->i_version; 467b71034e5SEvgeniy Dushistov need_revalidate = 0; 4681da177e4SLinus Torvalds } 469b71034e5SEvgeniy Dushistov de = (struct ufs_dir_entry *)(kaddr+offset); 470b71034e5SEvgeniy Dushistov limit = kaddr + ufs_last_byte(inode, n) - UFS_DIR_REC_LEN(1); 471b71034e5SEvgeniy Dushistov for ( ;(char*)de <= limit; de = ufs_next_entry(sb, de)) { 472b71034e5SEvgeniy Dushistov if (de->d_reclen == 0) { 4739746077aSHarvey Harrison ufs_error(sb, __func__, 474b71034e5SEvgeniy Dushistov "zero-length directory entry"); 475b71034e5SEvgeniy Dushistov ufs_put_page(page); 476b71034e5SEvgeniy Dushistov return -EIO; 4771da177e4SLinus Torvalds } 4781da177e4SLinus Torvalds if (de->d_ino) { 4791da177e4SLinus Torvalds unsigned char d_type = DT_UNKNOWN; 4801da177e4SLinus Torvalds 481abf5d15fSEvgeniy Dushistov UFSD("filldir(%s,%u)\n", de->d_name, 482abf5d15fSEvgeniy Dushistov fs32_to_cpu(sb, de->d_ino)); 483abf5d15fSEvgeniy Dushistov UFSD("namlen %u\n", ufs_get_de_namlen(sb, de)); 4841da177e4SLinus Torvalds 4851da177e4SLinus Torvalds if ((flags & UFS_DE_MASK) == UFS_DE_44BSD) 4861da177e4SLinus Torvalds d_type = de->d_u.d_44.d_type; 487b71034e5SEvgeniy Dushistov 48880886298SAl Viro if (!dir_emit(ctx, de->d_name, 489b71034e5SEvgeniy Dushistov ufs_get_de_namlen(sb, de), 49080886298SAl Viro fs32_to_cpu(sb, de->d_ino), 49180886298SAl Viro d_type)) { 492b71034e5SEvgeniy Dushistov ufs_put_page(page); 493b71034e5SEvgeniy Dushistov return 0; 494b71034e5SEvgeniy Dushistov } 4951da177e4SLinus Torvalds } 49680886298SAl Viro ctx->pos += fs16_to_cpu(sb, de->d_reclen); 4971da177e4SLinus Torvalds } 498b71034e5SEvgeniy Dushistov ufs_put_page(page); 4991da177e4SLinus Torvalds } 5001da177e4SLinus Torvalds return 0; 5011da177e4SLinus Torvalds } 5021da177e4SLinus Torvalds 5031da177e4SLinus Torvalds 5041da177e4SLinus Torvalds /* 5051da177e4SLinus Torvalds * ufs_delete_entry deletes a directory entry by merging it with the 5061da177e4SLinus Torvalds * previous entry. 5071da177e4SLinus Torvalds */ 5081da177e4SLinus Torvalds int ufs_delete_entry(struct inode *inode, struct ufs_dir_entry *dir, 509b71034e5SEvgeniy Dushistov struct page * page) 5101da177e4SLinus Torvalds { 511b71034e5SEvgeniy Dushistov struct super_block *sb = inode->i_sb; 512b71034e5SEvgeniy Dushistov char *kaddr = page_address(page); 513f336953bSEvgeniy Dushistov unsigned from = ((char*)dir - kaddr) & ~(UFS_SB(sb)->s_uspi->s_dirblksize - 1); 514b71034e5SEvgeniy Dushistov unsigned to = ((char*)dir - kaddr) + fs16_to_cpu(sb, dir->d_reclen); 51582b9d1d0SNick Piggin loff_t pos; 516b71034e5SEvgeniy Dushistov struct ufs_dir_entry *pde = NULL; 517b71034e5SEvgeniy Dushistov struct ufs_dir_entry *de = (struct ufs_dir_entry *) (kaddr + from); 518b71034e5SEvgeniy Dushistov int err; 5191da177e4SLinus Torvalds 520abf5d15fSEvgeniy Dushistov UFSD("ENTER\n"); 5211da177e4SLinus Torvalds 522abf5d15fSEvgeniy Dushistov UFSD("ino %u, reclen %u, namlen %u, name %s\n", 5231da177e4SLinus Torvalds fs32_to_cpu(sb, de->d_ino), 524221fc10eSEvgeniy fs16_to_cpu(sb, de->d_reclen), 525abf5d15fSEvgeniy Dushistov ufs_get_de_namlen(sb, de), de->d_name); 5261da177e4SLinus Torvalds 527b71034e5SEvgeniy Dushistov while ((char*)de < (char*)dir) { 528b71034e5SEvgeniy Dushistov if (de->d_reclen == 0) { 5299746077aSHarvey Harrison ufs_error(inode->i_sb, __func__, 530b71034e5SEvgeniy Dushistov "zero-length directory entry"); 531b71034e5SEvgeniy Dushistov err = -EIO; 532b71034e5SEvgeniy Dushistov goto out; 5331da177e4SLinus Torvalds } 534b71034e5SEvgeniy Dushistov pde = de; 535b71034e5SEvgeniy Dushistov de = ufs_next_entry(sb, de); 536b71034e5SEvgeniy Dushistov } 5371da177e4SLinus Torvalds if (pde) 538b71034e5SEvgeniy Dushistov from = (char*)pde - (char*)page_address(page); 53982b9d1d0SNick Piggin 54082b9d1d0SNick Piggin pos = page_offset(page) + from; 541b71034e5SEvgeniy Dushistov lock_page(page); 542f4e420dcSChristoph Hellwig err = ufs_prepare_chunk(page, pos, to - from); 543b71034e5SEvgeniy Dushistov BUG_ON(err); 544b71034e5SEvgeniy Dushistov if (pde) 545b71034e5SEvgeniy Dushistov pde->d_reclen = cpu_to_fs16(sb, to - from); 5461da177e4SLinus Torvalds dir->d_ino = 0; 54782b9d1d0SNick Piggin err = ufs_commit_chunk(page, pos, to - from); 5481da177e4SLinus Torvalds inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC; 5491da177e4SLinus Torvalds mark_inode_dirty(inode); 550b71034e5SEvgeniy Dushistov out: 551b71034e5SEvgeniy Dushistov ufs_put_page(page); 552abf5d15fSEvgeniy Dushistov UFSD("EXIT\n"); 553b71034e5SEvgeniy Dushistov return err; 5541da177e4SLinus Torvalds } 5551da177e4SLinus Torvalds 5561da177e4SLinus Torvalds int ufs_make_empty(struct inode * inode, struct inode *dir) 5571da177e4SLinus Torvalds { 5581da177e4SLinus Torvalds struct super_block * sb = dir->i_sb; 559b71034e5SEvgeniy Dushistov struct address_space *mapping = inode->i_mapping; 560b71034e5SEvgeniy Dushistov struct page *page = grab_cache_page(mapping, 0); 561f336953bSEvgeniy Dushistov const unsigned int chunk_size = UFS_SB(sb)->s_uspi->s_dirblksize; 5621da177e4SLinus Torvalds struct ufs_dir_entry * de; 563b71034e5SEvgeniy Dushistov char *base; 5641da177e4SLinus Torvalds int err; 5651da177e4SLinus Torvalds 566b71034e5SEvgeniy Dushistov if (!page) 567b71034e5SEvgeniy Dushistov return -ENOMEM; 56882b9d1d0SNick Piggin 569f4e420dcSChristoph Hellwig err = ufs_prepare_chunk(page, 0, chunk_size); 570b71034e5SEvgeniy Dushistov if (err) { 571b71034e5SEvgeniy Dushistov unlock_page(page); 572b71034e5SEvgeniy Dushistov goto fail; 573b71034e5SEvgeniy Dushistov } 5741da177e4SLinus Torvalds 57582b9d1d0SNick Piggin kmap(page); 576b71034e5SEvgeniy Dushistov base = (char*)page_address(page); 57709cbfeafSKirill A. Shutemov memset(base, 0, PAGE_SIZE); 578b71034e5SEvgeniy Dushistov 579b71034e5SEvgeniy Dushistov de = (struct ufs_dir_entry *) base; 580b71034e5SEvgeniy Dushistov 5811da177e4SLinus Torvalds de->d_ino = cpu_to_fs32(sb, inode->i_ino); 5821da177e4SLinus Torvalds ufs_set_de_type(sb, de, inode->i_mode); 5831da177e4SLinus Torvalds ufs_set_de_namlen(sb, de, 1); 5841da177e4SLinus Torvalds de->d_reclen = cpu_to_fs16(sb, UFS_DIR_REC_LEN(1)); 5851da177e4SLinus Torvalds strcpy (de->d_name, "."); 5861da177e4SLinus Torvalds de = (struct ufs_dir_entry *) 5871da177e4SLinus Torvalds ((char *)de + fs16_to_cpu(sb, de->d_reclen)); 5881da177e4SLinus Torvalds de->d_ino = cpu_to_fs32(sb, dir->i_ino); 5891da177e4SLinus Torvalds ufs_set_de_type(sb, de, dir->i_mode); 590f336953bSEvgeniy Dushistov de->d_reclen = cpu_to_fs16(sb, chunk_size - UFS_DIR_REC_LEN(1)); 5911da177e4SLinus Torvalds ufs_set_de_namlen(sb, de, 2); 5921da177e4SLinus Torvalds strcpy (de->d_name, ".."); 59382b9d1d0SNick Piggin kunmap(page); 594b71034e5SEvgeniy Dushistov 595f336953bSEvgeniy Dushistov err = ufs_commit_chunk(page, 0, chunk_size); 596b71034e5SEvgeniy Dushistov fail: 59709cbfeafSKirill A. Shutemov put_page(page); 598b71034e5SEvgeniy Dushistov return err; 5991da177e4SLinus Torvalds } 6001da177e4SLinus Torvalds 6011da177e4SLinus Torvalds /* 6021da177e4SLinus Torvalds * routine to check that the specified directory is empty (for rmdir) 6031da177e4SLinus Torvalds */ 6041da177e4SLinus Torvalds int ufs_empty_dir(struct inode * inode) 6051da177e4SLinus Torvalds { 606b71034e5SEvgeniy Dushistov struct super_block *sb = inode->i_sb; 607b71034e5SEvgeniy Dushistov struct page *page = NULL; 6085d754cedSFabian Frederick unsigned long i, npages = dir_pages(inode); 6091da177e4SLinus Torvalds 610b71034e5SEvgeniy Dushistov for (i = 0; i < npages; i++) { 611b71034e5SEvgeniy Dushistov char *kaddr; 612b71034e5SEvgeniy Dushistov struct ufs_dir_entry *de; 613b71034e5SEvgeniy Dushistov page = ufs_get_page(inode, i); 6141da177e4SLinus Torvalds 615b71034e5SEvgeniy Dushistov if (IS_ERR(page)) 6161da177e4SLinus Torvalds continue; 617b71034e5SEvgeniy Dushistov 618b71034e5SEvgeniy Dushistov kaddr = page_address(page); 619b71034e5SEvgeniy Dushistov de = (struct ufs_dir_entry *)kaddr; 620b71034e5SEvgeniy Dushistov kaddr += ufs_last_byte(inode, i) - UFS_DIR_REC_LEN(1); 621b71034e5SEvgeniy Dushistov 622b71034e5SEvgeniy Dushistov while ((char *)de <= kaddr) { 623b71034e5SEvgeniy Dushistov if (de->d_reclen == 0) { 6249746077aSHarvey Harrison ufs_error(inode->i_sb, __func__, 625b71034e5SEvgeniy Dushistov "zero-length directory entry: " 626b71034e5SEvgeniy Dushistov "kaddr=%p, de=%p\n", kaddr, de); 627b71034e5SEvgeniy Dushistov goto not_empty; 6281da177e4SLinus Torvalds } 6291da177e4SLinus Torvalds if (de->d_ino) { 630b71034e5SEvgeniy Dushistov u16 namelen=ufs_get_de_namlen(sb, de); 631b71034e5SEvgeniy Dushistov /* check for . and .. */ 632b71034e5SEvgeniy Dushistov if (de->d_name[0] != '.') 633b71034e5SEvgeniy Dushistov goto not_empty; 634b71034e5SEvgeniy Dushistov if (namelen > 2) 635b71034e5SEvgeniy Dushistov goto not_empty; 636b71034e5SEvgeniy Dushistov if (namelen < 2) { 637b71034e5SEvgeniy Dushistov if (inode->i_ino != 638b71034e5SEvgeniy Dushistov fs32_to_cpu(sb, de->d_ino)) 639b71034e5SEvgeniy Dushistov goto not_empty; 640b71034e5SEvgeniy Dushistov } else if (de->d_name[1] != '.') 641b71034e5SEvgeniy Dushistov goto not_empty; 6421da177e4SLinus Torvalds } 643b71034e5SEvgeniy Dushistov de = ufs_next_entry(sb, de); 6441da177e4SLinus Torvalds } 645b71034e5SEvgeniy Dushistov ufs_put_page(page); 646b71034e5SEvgeniy Dushistov } 6471da177e4SLinus Torvalds return 1; 648b71034e5SEvgeniy Dushistov 649b71034e5SEvgeniy Dushistov not_empty: 650b71034e5SEvgeniy Dushistov ufs_put_page(page); 651b71034e5SEvgeniy Dushistov return 0; 6521da177e4SLinus Torvalds } 6531da177e4SLinus Torvalds 6544b6f5d20SArjan van de Ven const struct file_operations ufs_dir_operations = { 6551da177e4SLinus Torvalds .read = generic_read_dir, 656*3b0a3c1aSAl Viro .iterate_shared = ufs_readdir, 6571b061d92SChristoph Hellwig .fsync = generic_file_fsync, 6583222a3e5SChristoph Hellwig .llseek = generic_file_llseek, 6591da177e4SLinus Torvalds }; 660