Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * linux/fs/ufs/ufs_dir.c |
| 4 | * |
| 5 | * Copyright (C) 1996 |
| 6 | * Adrian Rodriguez (adrian@franklins-tower.rutgers.edu) |
| 7 | * Laboratory for Computer Science Research Computing Facility |
| 8 | * Rutgers, The State University of New Jersey |
| 9 | * |
| 10 | * swab support by Francois-Rene Rideau <fare@tunes.org> 19970406 |
| 11 | * |
| 12 | * 4.4BSD (FreeBSD) support added on February 1st 1998 by |
| 13 | * Niels Kristian Bech Jensen <nkbj@image.dk> partially based |
| 14 | * on code by Martin von Loewis <martin@mira.isdn.cs.tu-berlin.de>. |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 15 | * |
| 16 | * Migration to usage of "page cache" on May 2006 by |
| 17 | * Evgeniy Dushistov <dushistov@mail.ru> based on ext2 code base. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | #include <linux/time.h> |
| 21 | #include <linux/fs.h> |
Nick Piggin | 82b9d1d | 2007-10-16 01:25:19 -0700 | [diff] [blame] | 22 | #include <linux/swap.h> |
Jeff Layton | bb8c2d6 | 2017-12-11 06:35:18 -0500 | [diff] [blame] | 23 | #include <linux/iversion.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
Mike Frysinger | e542059 | 2008-02-08 04:21:31 -0800 | [diff] [blame] | 25 | #include "ufs_fs.h" |
Christoph Hellwig | bcd6d4e | 2007-10-16 23:26:51 -0700 | [diff] [blame] | 26 | #include "ufs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include "swab.h" |
| 28 | #include "util.h" |
| 29 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | /* |
| 31 | * NOTE! unlike strncmp, ufs_match returns 1 for success, 0 for failure. |
| 32 | * |
| 33 | * len <= UFS_MAXNAMLEN and de != NULL are guaranteed by caller. |
| 34 | */ |
| 35 | static inline int ufs_match(struct super_block *sb, int len, |
Al Viro | 89031bc | 2010-01-31 20:49:54 -0500 | [diff] [blame] | 36 | const unsigned char *name, struct ufs_dir_entry *de) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | { |
| 38 | if (len != ufs_get_de_namlen(sb, de)) |
| 39 | return 0; |
| 40 | if (!de->d_ino) |
| 41 | return 0; |
| 42 | return !memcmp(name, de->d_name, len); |
| 43 | } |
| 44 | |
Nick Piggin | 82b9d1d | 2007-10-16 01:25:19 -0700 | [diff] [blame] | 45 | static int ufs_commit_chunk(struct page *page, loff_t pos, unsigned len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | { |
Nick Piggin | 82b9d1d | 2007-10-16 01:25:19 -0700 | [diff] [blame] | 47 | struct address_space *mapping = page->mapping; |
| 48 | struct inode *dir = mapping->host; |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 49 | int err = 0; |
Nick Piggin | 82b9d1d | 2007-10-16 01:25:19 -0700 | [diff] [blame] | 50 | |
Jeff Layton | bb8c2d6 | 2017-12-11 06:35:18 -0500 | [diff] [blame] | 51 | inode_inc_iversion(dir); |
Nick Piggin | 82b9d1d | 2007-10-16 01:25:19 -0700 | [diff] [blame] | 52 | block_write_end(NULL, mapping, pos, len, len, page, NULL); |
| 53 | if (pos+len > dir->i_size) { |
| 54 | i_size_write(dir, pos+len); |
| 55 | mark_inode_dirty(dir); |
| 56 | } |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 57 | if (IS_DIRSYNC(dir)) |
Jeff Layton | 2b69c82 | 2017-07-05 15:26:48 -0400 | [diff] [blame] | 58 | err = write_one_page(page); |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 59 | else |
| 60 | unlock_page(page); |
| 61 | return err; |
| 62 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 64 | static inline void ufs_put_page(struct page *page) |
| 65 | { |
| 66 | kunmap(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 67 | put_page(page); |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 68 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
Al Viro | 89031bc | 2010-01-31 20:49:54 -0500 | [diff] [blame] | 70 | ino_t ufs_inode_by_name(struct inode *dir, const struct qstr *qstr) |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 71 | { |
| 72 | ino_t res = 0; |
| 73 | struct ufs_dir_entry *de; |
| 74 | struct page *page; |
| 75 | |
Alexey Dobriyan | 0804970 | 2009-12-15 16:46:50 -0800 | [diff] [blame] | 76 | de = ufs_find_entry(dir, qstr, &page); |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 77 | if (de) { |
| 78 | res = fs32_to_cpu(dir->i_sb, de->d_ino); |
| 79 | ufs_put_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | } |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 81 | return res; |
| 82 | } |
| 83 | |
| 84 | |
| 85 | /* Releases the page */ |
| 86 | void ufs_set_link(struct inode *dir, struct ufs_dir_entry *de, |
Al Viro | 70d45cd | 2015-06-16 01:56:23 -0400 | [diff] [blame] | 87 | struct page *page, struct inode *inode, |
| 88 | bool update_times) |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 89 | { |
Nick Piggin | 82b9d1d | 2007-10-16 01:25:19 -0700 | [diff] [blame] | 90 | loff_t pos = page_offset(page) + |
| 91 | (char *) de - (char *) page_address(page); |
| 92 | unsigned len = fs16_to_cpu(dir->i_sb, de->d_reclen); |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 93 | int err; |
| 94 | |
| 95 | lock_page(page); |
Christoph Hellwig | f4e420d | 2010-06-04 11:29:56 +0200 | [diff] [blame] | 96 | err = ufs_prepare_chunk(page, pos, len); |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 97 | BUG_ON(err); |
Nick Piggin | 82b9d1d | 2007-10-16 01:25:19 -0700 | [diff] [blame] | 98 | |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 99 | de->d_ino = cpu_to_fs32(dir->i_sb, inode->i_ino); |
| 100 | ufs_set_de_type(dir->i_sb, de, inode->i_mode); |
Nick Piggin | 82b9d1d | 2007-10-16 01:25:19 -0700 | [diff] [blame] | 101 | |
| 102 | err = ufs_commit_chunk(page, pos, len); |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 103 | ufs_put_page(page); |
Al Viro | 70d45cd | 2015-06-16 01:56:23 -0400 | [diff] [blame] | 104 | if (update_times) |
Deepa Dinamani | 02027d4 | 2016-09-14 07:48:05 -0700 | [diff] [blame] | 105 | dir->i_mtime = dir->i_ctime = current_time(dir); |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 106 | mark_inode_dirty(dir); |
| 107 | } |
| 108 | |
| 109 | |
Al Viro | be5b82d | 2016-04-22 15:06:44 -0400 | [diff] [blame] | 110 | static bool ufs_check_page(struct page *page) |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 111 | { |
| 112 | struct inode *dir = page->mapping->host; |
| 113 | struct super_block *sb = dir->i_sb; |
| 114 | char *kaddr = page_address(page); |
| 115 | unsigned offs, rec_len; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 116 | unsigned limit = PAGE_SIZE; |
Evgeniy Dushistov | f336953 | 2007-02-08 14:20:25 -0800 | [diff] [blame] | 117 | const unsigned chunk_mask = UFS_SB(sb)->s_uspi->s_dirblksize - 1; |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 118 | struct ufs_dir_entry *p; |
| 119 | char *error; |
| 120 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 121 | if ((dir->i_size >> PAGE_SHIFT) == page->index) { |
| 122 | limit = dir->i_size & ~PAGE_MASK; |
Evgeniy Dushistov | f336953 | 2007-02-08 14:20:25 -0800 | [diff] [blame] | 123 | if (limit & chunk_mask) |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 124 | goto Ebadsize; |
| 125 | if (!limit) |
| 126 | goto out; |
| 127 | } |
| 128 | for (offs = 0; offs <= limit - UFS_DIR_REC_LEN(1); offs += rec_len) { |
| 129 | p = (struct ufs_dir_entry *)(kaddr + offs); |
| 130 | rec_len = fs16_to_cpu(sb, p->d_reclen); |
| 131 | |
| 132 | if (rec_len < UFS_DIR_REC_LEN(1)) |
| 133 | goto Eshort; |
| 134 | if (rec_len & 3) |
| 135 | goto Ealign; |
| 136 | if (rec_len < UFS_DIR_REC_LEN(ufs_get_de_namlen(sb, p))) |
| 137 | goto Enamelen; |
Evgeniy Dushistov | f336953 | 2007-02-08 14:20:25 -0800 | [diff] [blame] | 138 | if (((offs + rec_len - 1) ^ offs) & ~chunk_mask) |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 139 | goto Espan; |
| 140 | if (fs32_to_cpu(sb, p->d_ino) > (UFS_SB(sb)->s_uspi->s_ipg * |
| 141 | UFS_SB(sb)->s_uspi->s_ncg)) |
| 142 | goto Einumber; |
| 143 | } |
| 144 | if (offs != limit) |
| 145 | goto Eend; |
| 146 | out: |
| 147 | SetPageChecked(page); |
Al Viro | be5b82d | 2016-04-22 15:06:44 -0400 | [diff] [blame] | 148 | return true; |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 149 | |
| 150 | /* Too bad, we had an error */ |
| 151 | |
| 152 | Ebadsize: |
| 153 | ufs_error(sb, "ufs_check_page", |
| 154 | "size of directory #%lu is not a multiple of chunk size", |
| 155 | dir->i_ino |
| 156 | ); |
| 157 | goto fail; |
| 158 | Eshort: |
| 159 | error = "rec_len is smaller than minimal"; |
| 160 | goto bad_entry; |
| 161 | Ealign: |
| 162 | error = "unaligned directory entry"; |
| 163 | goto bad_entry; |
| 164 | Enamelen: |
| 165 | error = "rec_len is too small for name_len"; |
| 166 | goto bad_entry; |
| 167 | Espan: |
| 168 | error = "directory entry across blocks"; |
| 169 | goto bad_entry; |
| 170 | Einumber: |
| 171 | error = "inode out of bounds"; |
| 172 | bad_entry: |
| 173 | ufs_error (sb, "ufs_check_page", "bad entry in directory #%lu: %s - " |
| 174 | "offset=%lu, rec_len=%d, name_len=%d", |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 175 | dir->i_ino, error, (page->index<<PAGE_SHIFT)+offs, |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 176 | rec_len, ufs_get_de_namlen(sb, p)); |
| 177 | goto fail; |
| 178 | Eend: |
| 179 | p = (struct ufs_dir_entry *)(kaddr + offs); |
Harvey Harrison | 9746077a7 | 2008-04-28 02:16:17 -0700 | [diff] [blame] | 180 | ufs_error(sb, __func__, |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 181 | "entry in directory #%lu spans the page boundary" |
| 182 | "offset=%lu", |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 183 | dir->i_ino, (page->index<<PAGE_SHIFT)+offs); |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 184 | fail: |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 185 | SetPageError(page); |
Al Viro | be5b82d | 2016-04-22 15:06:44 -0400 | [diff] [blame] | 186 | return false; |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | static struct page *ufs_get_page(struct inode *dir, unsigned long n) |
| 190 | { |
| 191 | struct address_space *mapping = dir->i_mapping; |
Nick Piggin | 6fe6900 | 2007-05-06 14:49:04 -0700 | [diff] [blame] | 192 | struct page *page = read_mapping_page(mapping, n, NULL); |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 193 | if (!IS_ERR(page)) { |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 194 | kmap(page); |
Al Viro | be5b82d | 2016-04-22 15:06:44 -0400 | [diff] [blame] | 195 | if (unlikely(!PageChecked(page))) { |
| 196 | if (PageError(page) || !ufs_check_page(page)) |
| 197 | goto fail; |
| 198 | } |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 199 | } |
| 200 | return page; |
| 201 | |
| 202 | fail: |
| 203 | ufs_put_page(page); |
| 204 | return ERR_PTR(-EIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | /* |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 208 | * Return the offset into page `page_nr' of the last valid |
| 209 | * byte in that page, plus one. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | */ |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 211 | static unsigned |
| 212 | ufs_last_byte(struct inode *inode, unsigned long page_nr) |
| 213 | { |
| 214 | unsigned last_byte = inode->i_size; |
| 215 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 216 | last_byte -= page_nr << PAGE_SHIFT; |
| 217 | if (last_byte > PAGE_SIZE) |
| 218 | last_byte = PAGE_SIZE; |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 219 | return last_byte; |
| 220 | } |
| 221 | |
| 222 | static inline struct ufs_dir_entry * |
| 223 | ufs_next_entry(struct super_block *sb, struct ufs_dir_entry *p) |
| 224 | { |
| 225 | return (struct ufs_dir_entry *)((char *)p + |
| 226 | fs16_to_cpu(sb, p->d_reclen)); |
| 227 | } |
| 228 | |
| 229 | struct ufs_dir_entry *ufs_dotdot(struct inode *dir, struct page **p) |
| 230 | { |
| 231 | struct page *page = ufs_get_page(dir, 0); |
| 232 | struct ufs_dir_entry *de = NULL; |
| 233 | |
| 234 | if (!IS_ERR(page)) { |
| 235 | de = ufs_next_entry(dir->i_sb, |
| 236 | (struct ufs_dir_entry *)page_address(page)); |
| 237 | *p = page; |
| 238 | } |
| 239 | return de; |
| 240 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | |
| 242 | /* |
| 243 | * ufs_find_entry() |
| 244 | * |
| 245 | * finds an entry in the specified directory with the wanted name. It |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 246 | * returns the page in which the entry was found, and the entry itself |
| 247 | * (as a parameter - res_dir). Page is returned mapped and unlocked. |
| 248 | * Entry is guaranteed to be valid. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | */ |
Al Viro | 89031bc | 2010-01-31 20:49:54 -0500 | [diff] [blame] | 250 | struct ufs_dir_entry *ufs_find_entry(struct inode *dir, const struct qstr *qstr, |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 251 | struct page **res_page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | { |
| 253 | struct super_block *sb = dir->i_sb; |
Al Viro | 89031bc | 2010-01-31 20:49:54 -0500 | [diff] [blame] | 254 | const unsigned char *name = qstr->name; |
Alexey Dobriyan | 0804970 | 2009-12-15 16:46:50 -0800 | [diff] [blame] | 255 | int namelen = qstr->len; |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 256 | unsigned reclen = UFS_DIR_REC_LEN(namelen); |
| 257 | unsigned long start, n; |
Fabian Frederick | 5d754ce | 2015-05-24 17:19:43 +0200 | [diff] [blame] | 258 | unsigned long npages = dir_pages(dir); |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 259 | struct page *page = NULL; |
Evgeniy Dushistov | dd187a2 | 2006-06-25 05:47:25 -0700 | [diff] [blame] | 260 | struct ufs_inode_info *ui = UFS_I(dir); |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 261 | struct ufs_dir_entry *de; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | |
Evgeniy Dushistov | abf5d15 | 2006-06-25 05:47:24 -0700 | [diff] [blame] | 263 | UFSD("ENTER, dir_ino %lu, name %s, namlen %u\n", dir->i_ino, name, namelen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 265 | if (npages == 0 || namelen > UFS_MAXNAMLEN) |
| 266 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 268 | /* OFFSET_CACHE */ |
| 269 | *res_page = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | |
Evgeniy Dushistov | dd187a2 | 2006-06-25 05:47:25 -0700 | [diff] [blame] | 271 | start = ui->i_dir_start_lookup; |
| 272 | |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 273 | if (start >= npages) |
| 274 | start = 0; |
| 275 | n = start; |
| 276 | do { |
| 277 | char *kaddr; |
| 278 | page = ufs_get_page(dir, n); |
| 279 | if (!IS_ERR(page)) { |
| 280 | kaddr = page_address(page); |
| 281 | de = (struct ufs_dir_entry *) kaddr; |
| 282 | kaddr += ufs_last_byte(dir, n) - reclen; |
| 283 | while ((char *) de <= kaddr) { |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 284 | if (ufs_match(sb, namelen, name, de)) |
| 285 | goto found; |
| 286 | de = ufs_next_entry(sb, de); |
| 287 | } |
| 288 | ufs_put_page(page); |
| 289 | } |
| 290 | if (++n >= npages) |
| 291 | n = 0; |
| 292 | } while (n != start); |
| 293 | out: |
| 294 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 296 | found: |
| 297 | *res_page = page; |
Evgeniy Dushistov | dd187a2 | 2006-06-25 05:47:25 -0700 | [diff] [blame] | 298 | ui->i_dir_start_lookup = n; |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 299 | return de; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | /* |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 303 | * Parent is locked. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | */ |
| 305 | int ufs_add_link(struct dentry *dentry, struct inode *inode) |
| 306 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 307 | struct inode *dir = d_inode(dentry->d_parent); |
Al Viro | 89031bc | 2010-01-31 20:49:54 -0500 | [diff] [blame] | 308 | const unsigned char *name = dentry->d_name.name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | int namelen = dentry->d_name.len; |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 310 | struct super_block *sb = dir->i_sb; |
| 311 | unsigned reclen = UFS_DIR_REC_LEN(namelen); |
Evgeniy Dushistov | f336953 | 2007-02-08 14:20:25 -0800 | [diff] [blame] | 312 | const unsigned int chunk_size = UFS_SB(sb)->s_uspi->s_dirblksize; |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 313 | unsigned short rec_len, name_len; |
| 314 | struct page *page = NULL; |
| 315 | struct ufs_dir_entry *de; |
Fabian Frederick | 5d754ce | 2015-05-24 17:19:43 +0200 | [diff] [blame] | 316 | unsigned long npages = dir_pages(dir); |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 317 | unsigned long n; |
| 318 | char *kaddr; |
Nick Piggin | 82b9d1d | 2007-10-16 01:25:19 -0700 | [diff] [blame] | 319 | loff_t pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | int err; |
| 321 | |
Evgeniy Dushistov | abf5d15 | 2006-06-25 05:47:24 -0700 | [diff] [blame] | 322 | UFSD("ENTER, name %s, namelen %u\n", name, namelen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 324 | /* |
| 325 | * We take care of directory expansion in the same loop. |
| 326 | * This code plays outside i_size, so it locks the page |
| 327 | * to protect that region. |
| 328 | */ |
| 329 | for (n = 0; n <= npages; n++) { |
| 330 | char *dir_end; |
| 331 | |
| 332 | page = ufs_get_page(dir, n); |
| 333 | err = PTR_ERR(page); |
| 334 | if (IS_ERR(page)) |
| 335 | goto out; |
| 336 | lock_page(page); |
| 337 | kaddr = page_address(page); |
| 338 | dir_end = kaddr + ufs_last_byte(dir, n); |
| 339 | de = (struct ufs_dir_entry *)kaddr; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 340 | kaddr += PAGE_SIZE - reclen; |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 341 | while ((char *)de <= kaddr) { |
| 342 | if ((char *)de == dir_end) { |
| 343 | /* We hit i_size */ |
| 344 | name_len = 0; |
Evgeniy Dushistov | f336953 | 2007-02-08 14:20:25 -0800 | [diff] [blame] | 345 | rec_len = chunk_size; |
| 346 | de->d_reclen = cpu_to_fs16(sb, chunk_size); |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 347 | de->d_ino = 0; |
| 348 | goto got_it; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | } |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 350 | if (de->d_reclen == 0) { |
Harvey Harrison | 9746077a7 | 2008-04-28 02:16:17 -0700 | [diff] [blame] | 351 | ufs_error(dir->i_sb, __func__, |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 352 | "zero-length directory entry"); |
| 353 | err = -EIO; |
| 354 | goto out_unlock; |
| 355 | } |
| 356 | err = -EEXIST; |
| 357 | if (ufs_match(sb, namelen, name, de)) |
| 358 | goto out_unlock; |
| 359 | name_len = UFS_DIR_REC_LEN(ufs_get_de_namlen(sb, de)); |
| 360 | rec_len = fs16_to_cpu(sb, de->d_reclen); |
| 361 | if (!de->d_ino && rec_len >= reclen) |
| 362 | goto got_it; |
| 363 | if (rec_len >= name_len + reclen) |
| 364 | goto got_it; |
| 365 | de = (struct ufs_dir_entry *) ((char *) de + rec_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | } |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 367 | unlock_page(page); |
| 368 | ufs_put_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | } |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 370 | BUG(); |
| 371 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 373 | got_it: |
Nick Piggin | 82b9d1d | 2007-10-16 01:25:19 -0700 | [diff] [blame] | 374 | pos = page_offset(page) + |
| 375 | (char*)de - (char*)page_address(page); |
Christoph Hellwig | f4e420d | 2010-06-04 11:29:56 +0200 | [diff] [blame] | 376 | err = ufs_prepare_chunk(page, pos, rec_len); |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 377 | if (err) |
| 378 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | if (de->d_ino) { |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 380 | struct ufs_dir_entry *de1 = |
| 381 | (struct ufs_dir_entry *) ((char *) de + name_len); |
| 382 | de1->d_reclen = cpu_to_fs16(sb, rec_len - name_len); |
| 383 | de->d_reclen = cpu_to_fs16(sb, name_len); |
| 384 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | de = de1; |
| 386 | } |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 387 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | ufs_set_de_namlen(sb, de, namelen); |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 389 | memcpy(de->d_name, name, namelen + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | de->d_ino = cpu_to_fs32(sb, inode->i_ino); |
| 391 | ufs_set_de_type(sb, de, inode->i_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | |
Nick Piggin | 82b9d1d | 2007-10-16 01:25:19 -0700 | [diff] [blame] | 393 | err = ufs_commit_chunk(page, pos, rec_len); |
Deepa Dinamani | 02027d4 | 2016-09-14 07:48:05 -0700 | [diff] [blame] | 394 | dir->i_mtime = dir->i_ctime = current_time(dir); |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 395 | |
| 396 | mark_inode_dirty(dir); |
| 397 | /* OFFSET_CACHE */ |
| 398 | out_put: |
| 399 | ufs_put_page(page); |
| 400 | out: |
| 401 | return err; |
| 402 | out_unlock: |
| 403 | unlock_page(page); |
| 404 | goto out_put; |
| 405 | } |
| 406 | |
| 407 | static inline unsigned |
| 408 | ufs_validate_entry(struct super_block *sb, char *base, |
| 409 | unsigned offset, unsigned mask) |
| 410 | { |
| 411 | struct ufs_dir_entry *de = (struct ufs_dir_entry*)(base + offset); |
| 412 | struct ufs_dir_entry *p = (struct ufs_dir_entry*)(base + (offset&mask)); |
Al Viro | e0d508f | 2016-05-27 21:22:04 -0400 | [diff] [blame] | 413 | while ((char*)p < (char*)de) |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 414 | p = ufs_next_entry(sb, p); |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 415 | return (char *)p - base; |
| 416 | } |
| 417 | |
| 418 | |
| 419 | /* |
| 420 | * This is blatantly stolen from ext2fs |
| 421 | */ |
| 422 | static int |
Al Viro | 8088629 | 2013-05-15 18:51:49 -0400 | [diff] [blame] | 423 | ufs_readdir(struct file *file, struct dir_context *ctx) |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 424 | { |
Al Viro | 8088629 | 2013-05-15 18:51:49 -0400 | [diff] [blame] | 425 | loff_t pos = ctx->pos; |
| 426 | struct inode *inode = file_inode(file); |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 427 | struct super_block *sb = inode->i_sb; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 428 | unsigned int offset = pos & ~PAGE_MASK; |
| 429 | unsigned long n = pos >> PAGE_SHIFT; |
Fabian Frederick | 5d754ce | 2015-05-24 17:19:43 +0200 | [diff] [blame] | 430 | unsigned long npages = dir_pages(inode); |
Evgeniy Dushistov | f336953 | 2007-02-08 14:20:25 -0800 | [diff] [blame] | 431 | unsigned chunk_mask = ~(UFS_SB(sb)->s_uspi->s_dirblksize - 1); |
Goffredo Baroncelli | c472c07 | 2018-02-01 08:15:25 -0500 | [diff] [blame] | 432 | bool need_revalidate = !inode_eq_iversion(inode, file->f_version); |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 433 | unsigned flags = UFS_SB(sb)->s_flags; |
| 434 | |
Evgeniy Dushistov | abf5d15 | 2006-06-25 05:47:24 -0700 | [diff] [blame] | 435 | UFSD("BEGIN\n"); |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 436 | |
| 437 | if (pos > inode->i_size - UFS_DIR_REC_LEN(1)) |
| 438 | return 0; |
| 439 | |
| 440 | for ( ; n < npages; n++, offset = 0) { |
| 441 | char *kaddr, *limit; |
| 442 | struct ufs_dir_entry *de; |
| 443 | |
| 444 | struct page *page = ufs_get_page(inode, n); |
| 445 | |
| 446 | if (IS_ERR(page)) { |
Harvey Harrison | 9746077a7 | 2008-04-28 02:16:17 -0700 | [diff] [blame] | 447 | ufs_error(sb, __func__, |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 448 | "bad page in #%lu", |
| 449 | inode->i_ino); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 450 | ctx->pos += PAGE_SIZE - offset; |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 451 | return -EIO; |
| 452 | } |
| 453 | kaddr = page_address(page); |
| 454 | if (unlikely(need_revalidate)) { |
| 455 | if (offset) { |
| 456 | offset = ufs_validate_entry(sb, kaddr, offset, chunk_mask); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 457 | ctx->pos = (n<<PAGE_SHIFT) + offset; |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 458 | } |
Jeff Layton | bb8c2d6 | 2017-12-11 06:35:18 -0500 | [diff] [blame] | 459 | file->f_version = inode_query_iversion(inode); |
| 460 | need_revalidate = false; |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 461 | } |
| 462 | de = (struct ufs_dir_entry *)(kaddr+offset); |
| 463 | limit = kaddr + ufs_last_byte(inode, n) - UFS_DIR_REC_LEN(1); |
| 464 | for ( ;(char*)de <= limit; de = ufs_next_entry(sb, de)) { |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 465 | if (de->d_ino) { |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 466 | unsigned char d_type = DT_UNKNOWN; |
| 467 | |
Evgeniy Dushistov | abf5d15 | 2006-06-25 05:47:24 -0700 | [diff] [blame] | 468 | UFSD("filldir(%s,%u)\n", de->d_name, |
| 469 | fs32_to_cpu(sb, de->d_ino)); |
| 470 | UFSD("namlen %u\n", ufs_get_de_namlen(sb, de)); |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 471 | |
| 472 | if ((flags & UFS_DE_MASK) == UFS_DE_44BSD) |
| 473 | d_type = de->d_u.d_44.d_type; |
| 474 | |
Al Viro | 8088629 | 2013-05-15 18:51:49 -0400 | [diff] [blame] | 475 | if (!dir_emit(ctx, de->d_name, |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 476 | ufs_get_de_namlen(sb, de), |
Al Viro | 8088629 | 2013-05-15 18:51:49 -0400 | [diff] [blame] | 477 | fs32_to_cpu(sb, de->d_ino), |
| 478 | d_type)) { |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 479 | ufs_put_page(page); |
| 480 | return 0; |
| 481 | } |
| 482 | } |
Al Viro | 8088629 | 2013-05-15 18:51:49 -0400 | [diff] [blame] | 483 | ctx->pos += fs16_to_cpu(sb, de->d_reclen); |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 484 | } |
| 485 | ufs_put_page(page); |
| 486 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | return 0; |
| 488 | } |
| 489 | |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 490 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | /* |
| 492 | * ufs_delete_entry deletes a directory entry by merging it with the |
| 493 | * previous entry. |
| 494 | */ |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 495 | int ufs_delete_entry(struct inode *inode, struct ufs_dir_entry *dir, |
| 496 | struct page * page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | { |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 498 | struct super_block *sb = inode->i_sb; |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 499 | char *kaddr = page_address(page); |
Evgeniy Dushistov | f336953 | 2007-02-08 14:20:25 -0800 | [diff] [blame] | 500 | unsigned from = ((char*)dir - kaddr) & ~(UFS_SB(sb)->s_uspi->s_dirblksize - 1); |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 501 | unsigned to = ((char*)dir - kaddr) + fs16_to_cpu(sb, dir->d_reclen); |
Nick Piggin | 82b9d1d | 2007-10-16 01:25:19 -0700 | [diff] [blame] | 502 | loff_t pos; |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 503 | struct ufs_dir_entry *pde = NULL; |
| 504 | struct ufs_dir_entry *de = (struct ufs_dir_entry *) (kaddr + from); |
| 505 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | |
Evgeniy Dushistov | abf5d15 | 2006-06-25 05:47:24 -0700 | [diff] [blame] | 507 | UFSD("ENTER\n"); |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 508 | |
Evgeniy Dushistov | abf5d15 | 2006-06-25 05:47:24 -0700 | [diff] [blame] | 509 | UFSD("ino %u, reclen %u, namlen %u, name %s\n", |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 510 | fs32_to_cpu(sb, de->d_ino), |
| 511 | fs16_to_cpu(sb, de->d_reclen), |
Evgeniy Dushistov | abf5d15 | 2006-06-25 05:47:24 -0700 | [diff] [blame] | 512 | ufs_get_de_namlen(sb, de), de->d_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 514 | while ((char*)de < (char*)dir) { |
| 515 | if (de->d_reclen == 0) { |
Harvey Harrison | 9746077a7 | 2008-04-28 02:16:17 -0700 | [diff] [blame] | 516 | ufs_error(inode->i_sb, __func__, |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 517 | "zero-length directory entry"); |
| 518 | err = -EIO; |
| 519 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | } |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 521 | pde = de; |
| 522 | de = ufs_next_entry(sb, de); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | } |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 524 | if (pde) |
| 525 | from = (char*)pde - (char*)page_address(page); |
Nick Piggin | 82b9d1d | 2007-10-16 01:25:19 -0700 | [diff] [blame] | 526 | |
| 527 | pos = page_offset(page) + from; |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 528 | lock_page(page); |
Christoph Hellwig | f4e420d | 2010-06-04 11:29:56 +0200 | [diff] [blame] | 529 | err = ufs_prepare_chunk(page, pos, to - from); |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 530 | BUG_ON(err); |
| 531 | if (pde) |
Nick Piggin | 82b9d1d | 2007-10-16 01:25:19 -0700 | [diff] [blame] | 532 | pde->d_reclen = cpu_to_fs16(sb, to - from); |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 533 | dir->d_ino = 0; |
Nick Piggin | 82b9d1d | 2007-10-16 01:25:19 -0700 | [diff] [blame] | 534 | err = ufs_commit_chunk(page, pos, to - from); |
Deepa Dinamani | 02027d4 | 2016-09-14 07:48:05 -0700 | [diff] [blame] | 535 | inode->i_ctime = inode->i_mtime = current_time(inode); |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 536 | mark_inode_dirty(inode); |
| 537 | out: |
| 538 | ufs_put_page(page); |
Evgeniy Dushistov | abf5d15 | 2006-06-25 05:47:24 -0700 | [diff] [blame] | 539 | UFSD("EXIT\n"); |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 540 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | int ufs_make_empty(struct inode * inode, struct inode *dir) |
| 544 | { |
| 545 | struct super_block * sb = dir->i_sb; |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 546 | struct address_space *mapping = inode->i_mapping; |
| 547 | struct page *page = grab_cache_page(mapping, 0); |
Evgeniy Dushistov | f336953 | 2007-02-08 14:20:25 -0800 | [diff] [blame] | 548 | const unsigned int chunk_size = UFS_SB(sb)->s_uspi->s_dirblksize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | struct ufs_dir_entry * de; |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 550 | char *base; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | int err; |
| 552 | |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 553 | if (!page) |
| 554 | return -ENOMEM; |
Nick Piggin | 82b9d1d | 2007-10-16 01:25:19 -0700 | [diff] [blame] | 555 | |
Christoph Hellwig | f4e420d | 2010-06-04 11:29:56 +0200 | [diff] [blame] | 556 | err = ufs_prepare_chunk(page, 0, chunk_size); |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 557 | if (err) { |
| 558 | unlock_page(page); |
| 559 | goto fail; |
| 560 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | |
Nick Piggin | 82b9d1d | 2007-10-16 01:25:19 -0700 | [diff] [blame] | 562 | kmap(page); |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 563 | base = (char*)page_address(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 564 | memset(base, 0, PAGE_SIZE); |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 565 | |
| 566 | de = (struct ufs_dir_entry *) base; |
| 567 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | de->d_ino = cpu_to_fs32(sb, inode->i_ino); |
| 569 | ufs_set_de_type(sb, de, inode->i_mode); |
| 570 | ufs_set_de_namlen(sb, de, 1); |
| 571 | de->d_reclen = cpu_to_fs16(sb, UFS_DIR_REC_LEN(1)); |
| 572 | strcpy (de->d_name, "."); |
| 573 | de = (struct ufs_dir_entry *) |
| 574 | ((char *)de + fs16_to_cpu(sb, de->d_reclen)); |
| 575 | de->d_ino = cpu_to_fs32(sb, dir->i_ino); |
| 576 | ufs_set_de_type(sb, de, dir->i_mode); |
Evgeniy Dushistov | f336953 | 2007-02-08 14:20:25 -0800 | [diff] [blame] | 577 | de->d_reclen = cpu_to_fs16(sb, chunk_size - UFS_DIR_REC_LEN(1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | ufs_set_de_namlen(sb, de, 2); |
| 579 | strcpy (de->d_name, ".."); |
Nick Piggin | 82b9d1d | 2007-10-16 01:25:19 -0700 | [diff] [blame] | 580 | kunmap(page); |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 581 | |
Evgeniy Dushistov | f336953 | 2007-02-08 14:20:25 -0800 | [diff] [blame] | 582 | err = ufs_commit_chunk(page, 0, chunk_size); |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 583 | fail: |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 584 | put_page(page); |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 585 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | /* |
| 589 | * routine to check that the specified directory is empty (for rmdir) |
| 590 | */ |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 591 | int ufs_empty_dir(struct inode * inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | { |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 593 | struct super_block *sb = inode->i_sb; |
| 594 | struct page *page = NULL; |
Fabian Frederick | 5d754ce | 2015-05-24 17:19:43 +0200 | [diff] [blame] | 595 | unsigned long i, npages = dir_pages(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 597 | for (i = 0; i < npages; i++) { |
| 598 | char *kaddr; |
| 599 | struct ufs_dir_entry *de; |
| 600 | page = ufs_get_page(inode, i); |
| 601 | |
| 602 | if (IS_ERR(page)) |
| 603 | continue; |
| 604 | |
| 605 | kaddr = page_address(page); |
| 606 | de = (struct ufs_dir_entry *)kaddr; |
| 607 | kaddr += ufs_last_byte(inode, i) - UFS_DIR_REC_LEN(1); |
| 608 | |
| 609 | while ((char *)de <= kaddr) { |
| 610 | if (de->d_reclen == 0) { |
Harvey Harrison | 9746077a7 | 2008-04-28 02:16:17 -0700 | [diff] [blame] | 611 | ufs_error(inode->i_sb, __func__, |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 612 | "zero-length directory entry: " |
| 613 | "kaddr=%p, de=%p\n", kaddr, de); |
| 614 | goto not_empty; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | } |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 616 | if (de->d_ino) { |
| 617 | u16 namelen=ufs_get_de_namlen(sb, de); |
| 618 | /* check for . and .. */ |
| 619 | if (de->d_name[0] != '.') |
| 620 | goto not_empty; |
| 621 | if (namelen > 2) |
| 622 | goto not_empty; |
| 623 | if (namelen < 2) { |
| 624 | if (inode->i_ino != |
| 625 | fs32_to_cpu(sb, de->d_ino)) |
| 626 | goto not_empty; |
| 627 | } else if (de->d_name[1] != '.') |
| 628 | goto not_empty; |
| 629 | } |
| 630 | de = ufs_next_entry(sb, de); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | } |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 632 | ufs_put_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | return 1; |
Evgeniy Dushistov | b71034e | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 635 | |
| 636 | not_empty: |
| 637 | ufs_put_page(page); |
| 638 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | } |
| 640 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 641 | const struct file_operations ufs_dir_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | .read = generic_read_dir, |
Al Viro | 3b0a3c1 | 2016-04-20 23:42:46 -0400 | [diff] [blame] | 643 | .iterate_shared = ufs_readdir, |
Christoph Hellwig | 1b061d9 | 2010-05-26 17:53:41 +0200 | [diff] [blame] | 644 | .fsync = generic_file_fsync, |
Christoph Hellwig | 3222a3e | 2008-09-03 21:53:01 +0200 | [diff] [blame] | 645 | .llseek = generic_file_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | }; |