blob: 2edc1755b7c5dec73028ad04d5bbffff802d9f5a [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
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 Dushistovb71034e2006-06-25 05:47:22 -070015 *
16 * Migration to usage of "page cache" on May 2006 by
17 * Evgeniy Dushistov <dushistov@mail.ru> based on ext2 code base.
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 */
19
20#include <linux/time.h>
21#include <linux/fs.h>
Nick Piggin82b9d1d2007-10-16 01:25:19 -070022#include <linux/swap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Mike Frysingere5420592008-02-08 04:21:31 -080024#include "ufs_fs.h"
Christoph Hellwigbcd6d4e2007-10-16 23:26:51 -070025#include "ufs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "swab.h"
27#include "util.h"
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029/*
30 * NOTE! unlike strncmp, ufs_match returns 1 for success, 0 for failure.
31 *
32 * len <= UFS_MAXNAMLEN and de != NULL are guaranteed by caller.
33 */
34static inline int ufs_match(struct super_block *sb, int len,
Al Viro89031bc2010-01-31 20:49:54 -050035 const unsigned char *name, struct ufs_dir_entry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036{
37 if (len != ufs_get_de_namlen(sb, de))
38 return 0;
39 if (!de->d_ino)
40 return 0;
41 return !memcmp(name, de->d_name, len);
42}
43
Nick Piggin82b9d1d2007-10-16 01:25:19 -070044static int ufs_commit_chunk(struct page *page, loff_t pos, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
Nick Piggin82b9d1d2007-10-16 01:25:19 -070046 struct address_space *mapping = page->mapping;
47 struct inode *dir = mapping->host;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -070048 int err = 0;
Nick Piggin82b9d1d2007-10-16 01:25:19 -070049
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -070050 dir->i_version++;
Nick Piggin82b9d1d2007-10-16 01:25:19 -070051 block_write_end(NULL, mapping, pos, len, len, page, NULL);
52 if (pos+len > dir->i_size) {
53 i_size_write(dir, pos+len);
54 mark_inode_dirty(dir);
55 }
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -070056 if (IS_DIRSYNC(dir))
Jeff Layton2b69c822017-07-05 15:26:48 -040057 err = write_one_page(page);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -070058 else
59 unlock_page(page);
60 return err;
61}
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -070063static inline void ufs_put_page(struct page *page)
64{
65 kunmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030066 put_page(page);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -070067}
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Al Viro89031bc2010-01-31 20:49:54 -050069ino_t ufs_inode_by_name(struct inode *dir, const struct qstr *qstr)
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -070070{
71 ino_t res = 0;
72 struct ufs_dir_entry *de;
73 struct page *page;
74
Alexey Dobriyan08049702009-12-15 16:46:50 -080075 de = ufs_find_entry(dir, qstr, &page);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -070076 if (de) {
77 res = fs32_to_cpu(dir->i_sb, de->d_ino);
78 ufs_put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 }
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -070080 return res;
81}
82
83
84/* Releases the page */
85void ufs_set_link(struct inode *dir, struct ufs_dir_entry *de,
Al Viro70d45cd2015-06-16 01:56:23 -040086 struct page *page, struct inode *inode,
87 bool update_times)
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -070088{
Nick Piggin82b9d1d2007-10-16 01:25:19 -070089 loff_t pos = page_offset(page) +
90 (char *) de - (char *) page_address(page);
91 unsigned len = fs16_to_cpu(dir->i_sb, de->d_reclen);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -070092 int err;
93
94 lock_page(page);
Christoph Hellwigf4e420d2010-06-04 11:29:56 +020095 err = ufs_prepare_chunk(page, pos, len);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -070096 BUG_ON(err);
Nick Piggin82b9d1d2007-10-16 01:25:19 -070097
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -070098 de->d_ino = cpu_to_fs32(dir->i_sb, inode->i_ino);
99 ufs_set_de_type(dir->i_sb, de, inode->i_mode);
Nick Piggin82b9d1d2007-10-16 01:25:19 -0700100
101 err = ufs_commit_chunk(page, pos, len);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700102 ufs_put_page(page);
Al Viro70d45cd2015-06-16 01:56:23 -0400103 if (update_times)
Deepa Dinamani02027d42016-09-14 07:48:05 -0700104 dir->i_mtime = dir->i_ctime = current_time(dir);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700105 mark_inode_dirty(dir);
106}
107
108
Al Virobe5b82d2016-04-22 15:06:44 -0400109static bool ufs_check_page(struct page *page)
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700110{
111 struct inode *dir = page->mapping->host;
112 struct super_block *sb = dir->i_sb;
113 char *kaddr = page_address(page);
114 unsigned offs, rec_len;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300115 unsigned limit = PAGE_SIZE;
Evgeniy Dushistovf3369532007-02-08 14:20:25 -0800116 const unsigned chunk_mask = UFS_SB(sb)->s_uspi->s_dirblksize - 1;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700117 struct ufs_dir_entry *p;
118 char *error;
119
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300120 if ((dir->i_size >> PAGE_SHIFT) == page->index) {
121 limit = dir->i_size & ~PAGE_MASK;
Evgeniy Dushistovf3369532007-02-08 14:20:25 -0800122 if (limit & chunk_mask)
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700123 goto Ebadsize;
124 if (!limit)
125 goto out;
126 }
127 for (offs = 0; offs <= limit - UFS_DIR_REC_LEN(1); offs += rec_len) {
128 p = (struct ufs_dir_entry *)(kaddr + offs);
129 rec_len = fs16_to_cpu(sb, p->d_reclen);
130
131 if (rec_len < UFS_DIR_REC_LEN(1))
132 goto Eshort;
133 if (rec_len & 3)
134 goto Ealign;
135 if (rec_len < UFS_DIR_REC_LEN(ufs_get_de_namlen(sb, p)))
136 goto Enamelen;
Evgeniy Dushistovf3369532007-02-08 14:20:25 -0800137 if (((offs + rec_len - 1) ^ offs) & ~chunk_mask)
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700138 goto Espan;
139 if (fs32_to_cpu(sb, p->d_ino) > (UFS_SB(sb)->s_uspi->s_ipg *
140 UFS_SB(sb)->s_uspi->s_ncg))
141 goto Einumber;
142 }
143 if (offs != limit)
144 goto Eend;
145out:
146 SetPageChecked(page);
Al Virobe5b82d2016-04-22 15:06:44 -0400147 return true;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700148
149 /* Too bad, we had an error */
150
151Ebadsize:
152 ufs_error(sb, "ufs_check_page",
153 "size of directory #%lu is not a multiple of chunk size",
154 dir->i_ino
155 );
156 goto fail;
157Eshort:
158 error = "rec_len is smaller than minimal";
159 goto bad_entry;
160Ealign:
161 error = "unaligned directory entry";
162 goto bad_entry;
163Enamelen:
164 error = "rec_len is too small for name_len";
165 goto bad_entry;
166Espan:
167 error = "directory entry across blocks";
168 goto bad_entry;
169Einumber:
170 error = "inode out of bounds";
171bad_entry:
172 ufs_error (sb, "ufs_check_page", "bad entry in directory #%lu: %s - "
173 "offset=%lu, rec_len=%d, name_len=%d",
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300174 dir->i_ino, error, (page->index<<PAGE_SHIFT)+offs,
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700175 rec_len, ufs_get_de_namlen(sb, p));
176 goto fail;
177Eend:
178 p = (struct ufs_dir_entry *)(kaddr + offs);
Harvey Harrison9746077a72008-04-28 02:16:17 -0700179 ufs_error(sb, __func__,
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700180 "entry in directory #%lu spans the page boundary"
181 "offset=%lu",
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300182 dir->i_ino, (page->index<<PAGE_SHIFT)+offs);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700183fail:
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700184 SetPageError(page);
Al Virobe5b82d2016-04-22 15:06:44 -0400185 return false;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700186}
187
188static struct page *ufs_get_page(struct inode *dir, unsigned long n)
189{
190 struct address_space *mapping = dir->i_mapping;
Nick Piggin6fe69002007-05-06 14:49:04 -0700191 struct page *page = read_mapping_page(mapping, n, NULL);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700192 if (!IS_ERR(page)) {
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700193 kmap(page);
Al Virobe5b82d2016-04-22 15:06:44 -0400194 if (unlikely(!PageChecked(page))) {
195 if (PageError(page) || !ufs_check_page(page))
196 goto fail;
197 }
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700198 }
199 return page;
200
201fail:
202 ufs_put_page(page);
203 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
205
206/*
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700207 * Return the offset into page `page_nr' of the last valid
208 * byte in that page, plus one.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 */
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700210static unsigned
211ufs_last_byte(struct inode *inode, unsigned long page_nr)
212{
213 unsigned last_byte = inode->i_size;
214
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300215 last_byte -= page_nr << PAGE_SHIFT;
216 if (last_byte > PAGE_SIZE)
217 last_byte = PAGE_SIZE;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700218 return last_byte;
219}
220
221static inline struct ufs_dir_entry *
222ufs_next_entry(struct super_block *sb, struct ufs_dir_entry *p)
223{
224 return (struct ufs_dir_entry *)((char *)p +
225 fs16_to_cpu(sb, p->d_reclen));
226}
227
228struct ufs_dir_entry *ufs_dotdot(struct inode *dir, struct page **p)
229{
230 struct page *page = ufs_get_page(dir, 0);
231 struct ufs_dir_entry *de = NULL;
232
233 if (!IS_ERR(page)) {
234 de = ufs_next_entry(dir->i_sb,
235 (struct ufs_dir_entry *)page_address(page));
236 *p = page;
237 }
238 return de;
239}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241/*
242 * ufs_find_entry()
243 *
244 * finds an entry in the specified directory with the wanted name. It
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700245 * returns the page in which the entry was found, and the entry itself
246 * (as a parameter - res_dir). Page is returned mapped and unlocked.
247 * Entry is guaranteed to be valid.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 */
Al Viro89031bc2010-01-31 20:49:54 -0500249struct ufs_dir_entry *ufs_find_entry(struct inode *dir, const struct qstr *qstr,
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700250 struct page **res_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
252 struct super_block *sb = dir->i_sb;
Al Viro89031bc2010-01-31 20:49:54 -0500253 const unsigned char *name = qstr->name;
Alexey Dobriyan08049702009-12-15 16:46:50 -0800254 int namelen = qstr->len;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700255 unsigned reclen = UFS_DIR_REC_LEN(namelen);
256 unsigned long start, n;
Fabian Frederick5d754ce2015-05-24 17:19:43 +0200257 unsigned long npages = dir_pages(dir);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700258 struct page *page = NULL;
Evgeniy Dushistovdd187a22006-06-25 05:47:25 -0700259 struct ufs_inode_info *ui = UFS_I(dir);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700260 struct ufs_dir_entry *de;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700262 UFSD("ENTER, dir_ino %lu, name %s, namlen %u\n", dir->i_ino, name, namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700264 if (npages == 0 || namelen > UFS_MAXNAMLEN)
265 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700267 /* OFFSET_CACHE */
268 *res_page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Evgeniy Dushistovdd187a22006-06-25 05:47:25 -0700270 start = ui->i_dir_start_lookup;
271
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700272 if (start >= npages)
273 start = 0;
274 n = start;
275 do {
276 char *kaddr;
277 page = ufs_get_page(dir, n);
278 if (!IS_ERR(page)) {
279 kaddr = page_address(page);
280 de = (struct ufs_dir_entry *) kaddr;
281 kaddr += ufs_last_byte(dir, n) - reclen;
282 while ((char *) de <= kaddr) {
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700283 if (ufs_match(sb, namelen, name, de))
284 goto found;
285 de = ufs_next_entry(sb, de);
286 }
287 ufs_put_page(page);
288 }
289 if (++n >= npages)
290 n = 0;
291 } while (n != start);
292out:
293 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700295found:
296 *res_page = page;
Evgeniy Dushistovdd187a22006-06-25 05:47:25 -0700297 ui->i_dir_start_lookup = n;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700298 return de;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
300
301/*
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700302 * Parent is locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 */
304int ufs_add_link(struct dentry *dentry, struct inode *inode)
305{
David Howells2b0143b2015-03-17 22:25:59 +0000306 struct inode *dir = d_inode(dentry->d_parent);
Al Viro89031bc2010-01-31 20:49:54 -0500307 const unsigned char *name = dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 int namelen = dentry->d_name.len;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700309 struct super_block *sb = dir->i_sb;
310 unsigned reclen = UFS_DIR_REC_LEN(namelen);
Evgeniy Dushistovf3369532007-02-08 14:20:25 -0800311 const unsigned int chunk_size = UFS_SB(sb)->s_uspi->s_dirblksize;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700312 unsigned short rec_len, name_len;
313 struct page *page = NULL;
314 struct ufs_dir_entry *de;
Fabian Frederick5d754ce2015-05-24 17:19:43 +0200315 unsigned long npages = dir_pages(dir);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700316 unsigned long n;
317 char *kaddr;
Nick Piggin82b9d1d2007-10-16 01:25:19 -0700318 loff_t pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 int err;
320
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700321 UFSD("ENTER, name %s, namelen %u\n", name, namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700323 /*
324 * We take care of directory expansion in the same loop.
325 * This code plays outside i_size, so it locks the page
326 * to protect that region.
327 */
328 for (n = 0; n <= npages; n++) {
329 char *dir_end;
330
331 page = ufs_get_page(dir, n);
332 err = PTR_ERR(page);
333 if (IS_ERR(page))
334 goto out;
335 lock_page(page);
336 kaddr = page_address(page);
337 dir_end = kaddr + ufs_last_byte(dir, n);
338 de = (struct ufs_dir_entry *)kaddr;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300339 kaddr += PAGE_SIZE - reclen;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700340 while ((char *)de <= kaddr) {
341 if ((char *)de == dir_end) {
342 /* We hit i_size */
343 name_len = 0;
Evgeniy Dushistovf3369532007-02-08 14:20:25 -0800344 rec_len = chunk_size;
345 de->d_reclen = cpu_to_fs16(sb, chunk_size);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700346 de->d_ino = 0;
347 goto got_it;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 }
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700349 if (de->d_reclen == 0) {
Harvey Harrison9746077a72008-04-28 02:16:17 -0700350 ufs_error(dir->i_sb, __func__,
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700351 "zero-length directory entry");
352 err = -EIO;
353 goto out_unlock;
354 }
355 err = -EEXIST;
356 if (ufs_match(sb, namelen, name, de))
357 goto out_unlock;
358 name_len = UFS_DIR_REC_LEN(ufs_get_de_namlen(sb, de));
359 rec_len = fs16_to_cpu(sb, de->d_reclen);
360 if (!de->d_ino && rec_len >= reclen)
361 goto got_it;
362 if (rec_len >= name_len + reclen)
363 goto got_it;
364 de = (struct ufs_dir_entry *) ((char *) de + rec_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 }
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700366 unlock_page(page);
367 ufs_put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 }
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700369 BUG();
370 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700372got_it:
Nick Piggin82b9d1d2007-10-16 01:25:19 -0700373 pos = page_offset(page) +
374 (char*)de - (char*)page_address(page);
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200375 err = ufs_prepare_chunk(page, pos, rec_len);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700376 if (err)
377 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 if (de->d_ino) {
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700379 struct ufs_dir_entry *de1 =
380 (struct ufs_dir_entry *) ((char *) de + name_len);
381 de1->d_reclen = cpu_to_fs16(sb, rec_len - name_len);
382 de->d_reclen = cpu_to_fs16(sb, name_len);
383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 de = de1;
385 }
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 ufs_set_de_namlen(sb, de, namelen);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700388 memcpy(de->d_name, name, namelen + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 de->d_ino = cpu_to_fs32(sb, inode->i_ino);
390 ufs_set_de_type(sb, de, inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Nick Piggin82b9d1d2007-10-16 01:25:19 -0700392 err = ufs_commit_chunk(page, pos, rec_len);
Deepa Dinamani02027d42016-09-14 07:48:05 -0700393 dir->i_mtime = dir->i_ctime = current_time(dir);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700394
395 mark_inode_dirty(dir);
396 /* OFFSET_CACHE */
397out_put:
398 ufs_put_page(page);
399out:
400 return err;
401out_unlock:
402 unlock_page(page);
403 goto out_put;
404}
405
406static inline unsigned
407ufs_validate_entry(struct super_block *sb, char *base,
408 unsigned offset, unsigned mask)
409{
410 struct ufs_dir_entry *de = (struct ufs_dir_entry*)(base + offset);
411 struct ufs_dir_entry *p = (struct ufs_dir_entry*)(base + (offset&mask));
Al Viroe0d508f2016-05-27 21:22:04 -0400412 while ((char*)p < (char*)de)
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700413 p = ufs_next_entry(sb, p);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700414 return (char *)p - base;
415}
416
417
418/*
419 * This is blatantly stolen from ext2fs
420 */
421static int
Al Viro80886292013-05-15 18:51:49 -0400422ufs_readdir(struct file *file, struct dir_context *ctx)
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700423{
Al Viro80886292013-05-15 18:51:49 -0400424 loff_t pos = ctx->pos;
425 struct inode *inode = file_inode(file);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700426 struct super_block *sb = inode->i_sb;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300427 unsigned int offset = pos & ~PAGE_MASK;
428 unsigned long n = pos >> PAGE_SHIFT;
Fabian Frederick5d754ce2015-05-24 17:19:43 +0200429 unsigned long npages = dir_pages(inode);
Evgeniy Dushistovf3369532007-02-08 14:20:25 -0800430 unsigned chunk_mask = ~(UFS_SB(sb)->s_uspi->s_dirblksize - 1);
Al Viro80886292013-05-15 18:51:49 -0400431 int need_revalidate = file->f_version != inode->i_version;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700432 unsigned flags = UFS_SB(sb)->s_flags;
433
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700434 UFSD("BEGIN\n");
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700435
436 if (pos > inode->i_size - UFS_DIR_REC_LEN(1))
437 return 0;
438
439 for ( ; n < npages; n++, offset = 0) {
440 char *kaddr, *limit;
441 struct ufs_dir_entry *de;
442
443 struct page *page = ufs_get_page(inode, n);
444
445 if (IS_ERR(page)) {
Harvey Harrison9746077a72008-04-28 02:16:17 -0700446 ufs_error(sb, __func__,
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700447 "bad page in #%lu",
448 inode->i_ino);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300449 ctx->pos += PAGE_SIZE - offset;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700450 return -EIO;
451 }
452 kaddr = page_address(page);
453 if (unlikely(need_revalidate)) {
454 if (offset) {
455 offset = ufs_validate_entry(sb, kaddr, offset, chunk_mask);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300456 ctx->pos = (n<<PAGE_SHIFT) + offset;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700457 }
Al Viro80886292013-05-15 18:51:49 -0400458 file->f_version = inode->i_version;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700459 need_revalidate = 0;
460 }
461 de = (struct ufs_dir_entry *)(kaddr+offset);
462 limit = kaddr + ufs_last_byte(inode, n) - UFS_DIR_REC_LEN(1);
463 for ( ;(char*)de <= limit; de = ufs_next_entry(sb, de)) {
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700464 if (de->d_ino) {
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700465 unsigned char d_type = DT_UNKNOWN;
466
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700467 UFSD("filldir(%s,%u)\n", de->d_name,
468 fs32_to_cpu(sb, de->d_ino));
469 UFSD("namlen %u\n", ufs_get_de_namlen(sb, de));
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700470
471 if ((flags & UFS_DE_MASK) == UFS_DE_44BSD)
472 d_type = de->d_u.d_44.d_type;
473
Al Viro80886292013-05-15 18:51:49 -0400474 if (!dir_emit(ctx, de->d_name,
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700475 ufs_get_de_namlen(sb, de),
Al Viro80886292013-05-15 18:51:49 -0400476 fs32_to_cpu(sb, de->d_ino),
477 d_type)) {
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700478 ufs_put_page(page);
479 return 0;
480 }
481 }
Al Viro80886292013-05-15 18:51:49 -0400482 ctx->pos += fs16_to_cpu(sb, de->d_reclen);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700483 }
484 ufs_put_page(page);
485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 return 0;
487}
488
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490/*
491 * ufs_delete_entry deletes a directory entry by merging it with the
492 * previous entry.
493 */
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700494int ufs_delete_entry(struct inode *inode, struct ufs_dir_entry *dir,
495 struct page * page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700497 struct super_block *sb = inode->i_sb;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700498 char *kaddr = page_address(page);
Evgeniy Dushistovf3369532007-02-08 14:20:25 -0800499 unsigned from = ((char*)dir - kaddr) & ~(UFS_SB(sb)->s_uspi->s_dirblksize - 1);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700500 unsigned to = ((char*)dir - kaddr) + fs16_to_cpu(sb, dir->d_reclen);
Nick Piggin82b9d1d2007-10-16 01:25:19 -0700501 loff_t pos;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700502 struct ufs_dir_entry *pde = NULL;
503 struct ufs_dir_entry *de = (struct ufs_dir_entry *) (kaddr + from);
504 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700506 UFSD("ENTER\n");
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700507
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700508 UFSD("ino %u, reclen %u, namlen %u, name %s\n",
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700509 fs32_to_cpu(sb, de->d_ino),
510 fs16_to_cpu(sb, de->d_reclen),
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700511 ufs_get_de_namlen(sb, de), de->d_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700513 while ((char*)de < (char*)dir) {
514 if (de->d_reclen == 0) {
Harvey Harrison9746077a72008-04-28 02:16:17 -0700515 ufs_error(inode->i_sb, __func__,
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700516 "zero-length directory entry");
517 err = -EIO;
518 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 }
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700520 pde = de;
521 de = ufs_next_entry(sb, de);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 }
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700523 if (pde)
524 from = (char*)pde - (char*)page_address(page);
Nick Piggin82b9d1d2007-10-16 01:25:19 -0700525
526 pos = page_offset(page) + from;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700527 lock_page(page);
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200528 err = ufs_prepare_chunk(page, pos, to - from);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700529 BUG_ON(err);
530 if (pde)
Nick Piggin82b9d1d2007-10-16 01:25:19 -0700531 pde->d_reclen = cpu_to_fs16(sb, to - from);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700532 dir->d_ino = 0;
Nick Piggin82b9d1d2007-10-16 01:25:19 -0700533 err = ufs_commit_chunk(page, pos, to - from);
Deepa Dinamani02027d42016-09-14 07:48:05 -0700534 inode->i_ctime = inode->i_mtime = current_time(inode);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700535 mark_inode_dirty(inode);
536out:
537 ufs_put_page(page);
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700538 UFSD("EXIT\n");
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700539 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540}
541
542int ufs_make_empty(struct inode * inode, struct inode *dir)
543{
544 struct super_block * sb = dir->i_sb;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700545 struct address_space *mapping = inode->i_mapping;
546 struct page *page = grab_cache_page(mapping, 0);
Evgeniy Dushistovf3369532007-02-08 14:20:25 -0800547 const unsigned int chunk_size = UFS_SB(sb)->s_uspi->s_dirblksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 struct ufs_dir_entry * de;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700549 char *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 int err;
551
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700552 if (!page)
553 return -ENOMEM;
Nick Piggin82b9d1d2007-10-16 01:25:19 -0700554
Christoph Hellwigf4e420d2010-06-04 11:29:56 +0200555 err = ufs_prepare_chunk(page, 0, chunk_size);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700556 if (err) {
557 unlock_page(page);
558 goto fail;
559 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Nick Piggin82b9d1d2007-10-16 01:25:19 -0700561 kmap(page);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700562 base = (char*)page_address(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300563 memset(base, 0, PAGE_SIZE);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700564
565 de = (struct ufs_dir_entry *) base;
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 de->d_ino = cpu_to_fs32(sb, inode->i_ino);
568 ufs_set_de_type(sb, de, inode->i_mode);
569 ufs_set_de_namlen(sb, de, 1);
570 de->d_reclen = cpu_to_fs16(sb, UFS_DIR_REC_LEN(1));
571 strcpy (de->d_name, ".");
572 de = (struct ufs_dir_entry *)
573 ((char *)de + fs16_to_cpu(sb, de->d_reclen));
574 de->d_ino = cpu_to_fs32(sb, dir->i_ino);
575 ufs_set_de_type(sb, de, dir->i_mode);
Evgeniy Dushistovf3369532007-02-08 14:20:25 -0800576 de->d_reclen = cpu_to_fs16(sb, chunk_size - UFS_DIR_REC_LEN(1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 ufs_set_de_namlen(sb, de, 2);
578 strcpy (de->d_name, "..");
Nick Piggin82b9d1d2007-10-16 01:25:19 -0700579 kunmap(page);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700580
Evgeniy Dushistovf3369532007-02-08 14:20:25 -0800581 err = ufs_commit_chunk(page, 0, chunk_size);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700582fail:
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300583 put_page(page);
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700584 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585}
586
587/*
588 * routine to check that the specified directory is empty (for rmdir)
589 */
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700590int ufs_empty_dir(struct inode * inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700592 struct super_block *sb = inode->i_sb;
593 struct page *page = NULL;
Fabian Frederick5d754ce2015-05-24 17:19:43 +0200594 unsigned long i, npages = dir_pages(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700596 for (i = 0; i < npages; i++) {
597 char *kaddr;
598 struct ufs_dir_entry *de;
599 page = ufs_get_page(inode, i);
600
601 if (IS_ERR(page))
602 continue;
603
604 kaddr = page_address(page);
605 de = (struct ufs_dir_entry *)kaddr;
606 kaddr += ufs_last_byte(inode, i) - UFS_DIR_REC_LEN(1);
607
608 while ((char *)de <= kaddr) {
609 if (de->d_reclen == 0) {
Harvey Harrison9746077a72008-04-28 02:16:17 -0700610 ufs_error(inode->i_sb, __func__,
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700611 "zero-length directory entry: "
612 "kaddr=%p, de=%p\n", kaddr, de);
613 goto not_empty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 }
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700615 if (de->d_ino) {
616 u16 namelen=ufs_get_de_namlen(sb, de);
617 /* check for . and .. */
618 if (de->d_name[0] != '.')
619 goto not_empty;
620 if (namelen > 2)
621 goto not_empty;
622 if (namelen < 2) {
623 if (inode->i_ino !=
624 fs32_to_cpu(sb, de->d_ino))
625 goto not_empty;
626 } else if (de->d_name[1] != '.')
627 goto not_empty;
628 }
629 de = ufs_next_entry(sb, de);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 }
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700631 ufs_put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 return 1;
Evgeniy Dushistovb71034e2006-06-25 05:47:22 -0700634
635not_empty:
636 ufs_put_page(page);
637 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638}
639
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800640const struct file_operations ufs_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 .read = generic_read_dir,
Al Viro3b0a3c12016-04-20 23:42:46 -0400642 .iterate_shared = ufs_readdir,
Christoph Hellwig1b061d92010-05-26 17:53:41 +0200643 .fsync = generic_file_fsync,
Christoph Hellwig3222a3e2008-09-03 21:53:01 +0200644 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645};