1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * linux/fs/ufs/ufs_dir.c
41da177e4SLinus Torvalds *
51da177e4SLinus Torvalds * Copyright (C) 1996
61da177e4SLinus Torvalds * Adrian Rodriguez (adrian@franklins-tower.rutgers.edu)
71da177e4SLinus Torvalds * Laboratory for Computer Science Research Computing Facility
81da177e4SLinus Torvalds * Rutgers, The State University of New Jersey
91da177e4SLinus Torvalds *
101da177e4SLinus Torvalds * swab support by Francois-Rene Rideau <fare@tunes.org> 19970406
111da177e4SLinus Torvalds *
121da177e4SLinus Torvalds * 4.4BSD (FreeBSD) support added on February 1st 1998 by
131da177e4SLinus Torvalds * Niels Kristian Bech Jensen <nkbj@image.dk> partially based
141da177e4SLinus Torvalds * on code by Martin von Loewis <martin@mira.isdn.cs.tu-berlin.de>.
15b71034e5SEvgeniy Dushistov *
16b71034e5SEvgeniy Dushistov * Migration to usage of "page cache" on May 2006 by
17b71034e5SEvgeniy Dushistov * Evgeniy Dushistov <dushistov@mail.ru> based on ext2 code base.
181da177e4SLinus Torvalds */
191da177e4SLinus Torvalds
201da177e4SLinus Torvalds #include <linux/time.h>
211da177e4SLinus Torvalds #include <linux/fs.h>
2282b9d1d0SNick Piggin #include <linux/swap.h>
23bb8c2d66SJeff Layton #include <linux/iversion.h>
241da177e4SLinus Torvalds
25e5420598SMike Frysinger #include "ufs_fs.h"
26bcd6d4ecSChristoph Hellwig #include "ufs.h"
271da177e4SLinus Torvalds #include "swab.h"
281da177e4SLinus Torvalds #include "util.h"
291da177e4SLinus Torvalds
301da177e4SLinus Torvalds /*
311da177e4SLinus Torvalds * NOTE! unlike strncmp, ufs_match returns 1 for success, 0 for failure.
321da177e4SLinus Torvalds *
331da177e4SLinus Torvalds * len <= UFS_MAXNAMLEN and de != NULL are guaranteed by caller.
341da177e4SLinus Torvalds */
ufs_match(struct super_block * sb,int len,const unsigned char * name,struct ufs_dir_entry * de)351da177e4SLinus Torvalds static inline int ufs_match(struct super_block *sb, int len,
3689031bc7SAl Viro const unsigned char *name, struct ufs_dir_entry *de)
371da177e4SLinus Torvalds {
381da177e4SLinus Torvalds if (len != ufs_get_de_namlen(sb, de))
391da177e4SLinus Torvalds return 0;
401da177e4SLinus Torvalds if (!de->d_ino)
411da177e4SLinus Torvalds return 0;
421da177e4SLinus Torvalds return !memcmp(name, de->d_name, len);
431da177e4SLinus Torvalds }
441da177e4SLinus Torvalds
ufs_commit_chunk(struct page * page,loff_t pos,unsigned len)459e22031aSChristoph Hellwig static void ufs_commit_chunk(struct page *page, loff_t pos, unsigned len)
46b71034e5SEvgeniy Dushistov {
4782b9d1d0SNick Piggin struct address_space *mapping = page->mapping;
4882b9d1d0SNick Piggin struct inode *dir = mapping->host;
4982b9d1d0SNick Piggin
50bb8c2d66SJeff Layton inode_inc_iversion(dir);
5182b9d1d0SNick Piggin block_write_end(NULL, mapping, pos, len, len, page, NULL);
5282b9d1d0SNick Piggin if (pos+len > dir->i_size) {
5382b9d1d0SNick Piggin i_size_write(dir, pos+len);
5482b9d1d0SNick Piggin mark_inode_dirty(dir);
5582b9d1d0SNick Piggin }
56b71034e5SEvgeniy Dushistov unlock_page(page);
579e22031aSChristoph Hellwig }
589e22031aSChristoph Hellwig
ufs_handle_dirsync(struct inode * dir)599e22031aSChristoph Hellwig static int ufs_handle_dirsync(struct inode *dir)
609e22031aSChristoph Hellwig {
619e22031aSChristoph Hellwig int err;
629e22031aSChristoph Hellwig
639e22031aSChristoph Hellwig err = filemap_write_and_wait(dir->i_mapping);
649e22031aSChristoph Hellwig if (!err)
659e22031aSChristoph Hellwig err = sync_inode_metadata(dir, 1);
66b71034e5SEvgeniy Dushistov return err;
67b71034e5SEvgeniy Dushistov }
68b71034e5SEvgeniy Dushistov
ufs_put_page(struct page * page)69b71034e5SEvgeniy Dushistov static inline void ufs_put_page(struct page *page)
70b71034e5SEvgeniy Dushistov {
71b71034e5SEvgeniy Dushistov kunmap(page);
7209cbfeafSKirill A. Shutemov put_page(page);
73b71034e5SEvgeniy Dushistov }
74b71034e5SEvgeniy Dushistov
ufs_inode_by_name(struct inode * dir,const struct qstr * qstr)7589031bc7SAl Viro ino_t ufs_inode_by_name(struct inode *dir, const struct qstr *qstr)
76b71034e5SEvgeniy Dushistov {
77b71034e5SEvgeniy Dushistov ino_t res = 0;
78b71034e5SEvgeniy Dushistov struct ufs_dir_entry *de;
79b71034e5SEvgeniy Dushistov struct page *page;
80b71034e5SEvgeniy Dushistov
8108049707SAlexey Dobriyan de = ufs_find_entry(dir, qstr, &page);
82b71034e5SEvgeniy Dushistov if (de) {
83b71034e5SEvgeniy Dushistov res = fs32_to_cpu(dir->i_sb, de->d_ino);
84b71034e5SEvgeniy Dushistov ufs_put_page(page);
85b71034e5SEvgeniy Dushistov }
86b71034e5SEvgeniy Dushistov return res;
87b71034e5SEvgeniy Dushistov }
88b71034e5SEvgeniy Dushistov
89b71034e5SEvgeniy Dushistov
90b71034e5SEvgeniy Dushistov /* Releases the page */
ufs_set_link(struct inode * dir,struct ufs_dir_entry * de,struct page * page,struct inode * inode,bool update_times)91b71034e5SEvgeniy Dushistov void ufs_set_link(struct inode *dir, struct ufs_dir_entry *de,
9270d45cdbSAl Viro struct page *page, struct inode *inode,
9370d45cdbSAl Viro bool update_times)
94b71034e5SEvgeniy Dushistov {
9582b9d1d0SNick Piggin loff_t pos = page_offset(page) +
9682b9d1d0SNick Piggin (char *) de - (char *) page_address(page);
9782b9d1d0SNick Piggin unsigned len = fs16_to_cpu(dir->i_sb, de->d_reclen);
98b71034e5SEvgeniy Dushistov int err;
99b71034e5SEvgeniy Dushistov
100b71034e5SEvgeniy Dushistov lock_page(page);
101f4e420dcSChristoph Hellwig err = ufs_prepare_chunk(page, pos, len);
102b71034e5SEvgeniy Dushistov BUG_ON(err);
10382b9d1d0SNick Piggin
104b71034e5SEvgeniy Dushistov de->d_ino = cpu_to_fs32(dir->i_sb, inode->i_ino);
105b71034e5SEvgeniy Dushistov ufs_set_de_type(dir->i_sb, de, inode->i_mode);
10682b9d1d0SNick Piggin
1079e22031aSChristoph Hellwig ufs_commit_chunk(page, pos, len);
108b71034e5SEvgeniy Dushistov ufs_put_page(page);
10970d45cdbSAl Viro if (update_times)
110*6eeb017eSJeff Layton dir->i_mtime = inode_set_ctime_current(dir);
111b71034e5SEvgeniy Dushistov mark_inode_dirty(dir);
1129e22031aSChristoph Hellwig ufs_handle_dirsync(dir);
113b71034e5SEvgeniy Dushistov }
114b71034e5SEvgeniy Dushistov
115b71034e5SEvgeniy Dushistov
ufs_check_page(struct page * page)116be5b82dbSAl Viro static bool ufs_check_page(struct page *page)
117b71034e5SEvgeniy Dushistov {
118b71034e5SEvgeniy Dushistov struct inode *dir = page->mapping->host;
119b71034e5SEvgeniy Dushistov struct super_block *sb = dir->i_sb;
120b71034e5SEvgeniy Dushistov char *kaddr = page_address(page);
121b71034e5SEvgeniy Dushistov unsigned offs, rec_len;
12209cbfeafSKirill A. Shutemov unsigned limit = PAGE_SIZE;
123f336953bSEvgeniy Dushistov const unsigned chunk_mask = UFS_SB(sb)->s_uspi->s_dirblksize - 1;
124b71034e5SEvgeniy Dushistov struct ufs_dir_entry *p;
125b71034e5SEvgeniy Dushistov char *error;
126b71034e5SEvgeniy Dushistov
12709cbfeafSKirill A. Shutemov if ((dir->i_size >> PAGE_SHIFT) == page->index) {
12809cbfeafSKirill A. Shutemov limit = dir->i_size & ~PAGE_MASK;
129f336953bSEvgeniy Dushistov if (limit & chunk_mask)
130b71034e5SEvgeniy Dushistov goto Ebadsize;
131b71034e5SEvgeniy Dushistov if (!limit)
132b71034e5SEvgeniy Dushistov goto out;
133b71034e5SEvgeniy Dushistov }
134b71034e5SEvgeniy Dushistov for (offs = 0; offs <= limit - UFS_DIR_REC_LEN(1); offs += rec_len) {
135b71034e5SEvgeniy Dushistov p = (struct ufs_dir_entry *)(kaddr + offs);
136b71034e5SEvgeniy Dushistov rec_len = fs16_to_cpu(sb, p->d_reclen);
137b71034e5SEvgeniy Dushistov
138b71034e5SEvgeniy Dushistov if (rec_len < UFS_DIR_REC_LEN(1))
139b71034e5SEvgeniy Dushistov goto Eshort;
140b71034e5SEvgeniy Dushistov if (rec_len & 3)
141b71034e5SEvgeniy Dushistov goto Ealign;
142b71034e5SEvgeniy Dushistov if (rec_len < UFS_DIR_REC_LEN(ufs_get_de_namlen(sb, p)))
143b71034e5SEvgeniy Dushistov goto Enamelen;
144f336953bSEvgeniy Dushistov if (((offs + rec_len - 1) ^ offs) & ~chunk_mask)
145b71034e5SEvgeniy Dushistov goto Espan;
146b71034e5SEvgeniy Dushistov if (fs32_to_cpu(sb, p->d_ino) > (UFS_SB(sb)->s_uspi->s_ipg *
147b71034e5SEvgeniy Dushistov UFS_SB(sb)->s_uspi->s_ncg))
148b71034e5SEvgeniy Dushistov goto Einumber;
149b71034e5SEvgeniy Dushistov }
150b71034e5SEvgeniy Dushistov if (offs != limit)
151b71034e5SEvgeniy Dushistov goto Eend;
152b71034e5SEvgeniy Dushistov out:
153b71034e5SEvgeniy Dushistov SetPageChecked(page);
154be5b82dbSAl Viro return true;
155b71034e5SEvgeniy Dushistov
156b71034e5SEvgeniy Dushistov /* Too bad, we had an error */
157b71034e5SEvgeniy Dushistov
158b71034e5SEvgeniy Dushistov Ebadsize:
159b71034e5SEvgeniy Dushistov ufs_error(sb, "ufs_check_page",
160b71034e5SEvgeniy Dushistov "size of directory #%lu is not a multiple of chunk size",
161b71034e5SEvgeniy Dushistov dir->i_ino
162b71034e5SEvgeniy Dushistov );
163b71034e5SEvgeniy Dushistov goto fail;
164b71034e5SEvgeniy Dushistov Eshort:
165b71034e5SEvgeniy Dushistov error = "rec_len is smaller than minimal";
166b71034e5SEvgeniy Dushistov goto bad_entry;
167b71034e5SEvgeniy Dushistov Ealign:
168b71034e5SEvgeniy Dushistov error = "unaligned directory entry";
169b71034e5SEvgeniy Dushistov goto bad_entry;
170b71034e5SEvgeniy Dushistov Enamelen:
171b71034e5SEvgeniy Dushistov error = "rec_len is too small for name_len";
172b71034e5SEvgeniy Dushistov goto bad_entry;
173b71034e5SEvgeniy Dushistov Espan:
174b71034e5SEvgeniy Dushistov error = "directory entry across blocks";
175b71034e5SEvgeniy Dushistov goto bad_entry;
176b71034e5SEvgeniy Dushistov Einumber:
177b71034e5SEvgeniy Dushistov error = "inode out of bounds";
178b71034e5SEvgeniy Dushistov bad_entry:
179b71034e5SEvgeniy Dushistov ufs_error (sb, "ufs_check_page", "bad entry in directory #%lu: %s - "
180b71034e5SEvgeniy Dushistov "offset=%lu, rec_len=%d, name_len=%d",
18109cbfeafSKirill A. Shutemov dir->i_ino, error, (page->index<<PAGE_SHIFT)+offs,
182b71034e5SEvgeniy Dushistov rec_len, ufs_get_de_namlen(sb, p));
183b71034e5SEvgeniy Dushistov goto fail;
184b71034e5SEvgeniy Dushistov Eend:
185b71034e5SEvgeniy Dushistov p = (struct ufs_dir_entry *)(kaddr + offs);
1869746077aSHarvey Harrison ufs_error(sb, __func__,
187b71034e5SEvgeniy Dushistov "entry in directory #%lu spans the page boundary"
188b71034e5SEvgeniy Dushistov "offset=%lu",
18909cbfeafSKirill A. Shutemov dir->i_ino, (page->index<<PAGE_SHIFT)+offs);
190b71034e5SEvgeniy Dushistov fail:
191b71034e5SEvgeniy Dushistov SetPageError(page);
192be5b82dbSAl Viro return false;
193b71034e5SEvgeniy Dushistov }
194b71034e5SEvgeniy Dushistov
ufs_get_page(struct inode * dir,unsigned long n)195b71034e5SEvgeniy Dushistov static struct page *ufs_get_page(struct inode *dir, unsigned long n)
196b71034e5SEvgeniy Dushistov {
197b71034e5SEvgeniy Dushistov struct address_space *mapping = dir->i_mapping;
1986fe6900eSNick Piggin struct page *page = read_mapping_page(mapping, n, NULL);
199b71034e5SEvgeniy Dushistov if (!IS_ERR(page)) {
200b71034e5SEvgeniy Dushistov kmap(page);
201be5b82dbSAl Viro if (unlikely(!PageChecked(page))) {
202771075e1SMatthew Wilcox (Oracle) if (!ufs_check_page(page))
203b71034e5SEvgeniy Dushistov goto fail;
204b71034e5SEvgeniy Dushistov }
205be5b82dbSAl Viro }
206b71034e5SEvgeniy Dushistov return page;
207b71034e5SEvgeniy Dushistov
208b71034e5SEvgeniy Dushistov fail:
209b71034e5SEvgeniy Dushistov ufs_put_page(page);
210b71034e5SEvgeniy Dushistov return ERR_PTR(-EIO);
211b71034e5SEvgeniy Dushistov }
212b71034e5SEvgeniy Dushistov
213b71034e5SEvgeniy Dushistov /*
214b71034e5SEvgeniy Dushistov * Return the offset into page `page_nr' of the last valid
215b71034e5SEvgeniy Dushistov * byte in that page, plus one.
216b71034e5SEvgeniy Dushistov */
217b71034e5SEvgeniy Dushistov static unsigned
ufs_last_byte(struct inode * inode,unsigned long page_nr)218b71034e5SEvgeniy Dushistov ufs_last_byte(struct inode *inode, unsigned long page_nr)
219b71034e5SEvgeniy Dushistov {
220b71034e5SEvgeniy Dushistov unsigned last_byte = inode->i_size;
221b71034e5SEvgeniy Dushistov
22209cbfeafSKirill A. Shutemov last_byte -= page_nr << PAGE_SHIFT;
22309cbfeafSKirill A. Shutemov if (last_byte > PAGE_SIZE)
22409cbfeafSKirill A. Shutemov last_byte = PAGE_SIZE;
225b71034e5SEvgeniy Dushistov return last_byte;
226b71034e5SEvgeniy Dushistov }
227b71034e5SEvgeniy Dushistov
228b71034e5SEvgeniy Dushistov static inline struct ufs_dir_entry *
ufs_next_entry(struct super_block * sb,struct ufs_dir_entry * p)229b71034e5SEvgeniy Dushistov ufs_next_entry(struct super_block *sb, struct ufs_dir_entry *p)
230b71034e5SEvgeniy Dushistov {
231b71034e5SEvgeniy Dushistov return (struct ufs_dir_entry *)((char *)p +
232b71034e5SEvgeniy Dushistov fs16_to_cpu(sb, p->d_reclen));
233b71034e5SEvgeniy Dushistov }
234b71034e5SEvgeniy Dushistov
ufs_dotdot(struct inode * dir,struct page ** p)235b71034e5SEvgeniy Dushistov struct ufs_dir_entry *ufs_dotdot(struct inode *dir, struct page **p)
236b71034e5SEvgeniy Dushistov {
237b71034e5SEvgeniy Dushistov struct page *page = ufs_get_page(dir, 0);
238b71034e5SEvgeniy Dushistov struct ufs_dir_entry *de = NULL;
239b71034e5SEvgeniy Dushistov
240b71034e5SEvgeniy Dushistov if (!IS_ERR(page)) {
241b71034e5SEvgeniy Dushistov de = ufs_next_entry(dir->i_sb,
242b71034e5SEvgeniy Dushistov (struct ufs_dir_entry *)page_address(page));
243b71034e5SEvgeniy Dushistov *p = page;
244b71034e5SEvgeniy Dushistov }
245b71034e5SEvgeniy Dushistov return de;
246b71034e5SEvgeniy Dushistov }
247b71034e5SEvgeniy Dushistov
248b71034e5SEvgeniy Dushistov /*
249b71034e5SEvgeniy Dushistov * ufs_find_entry()
250b71034e5SEvgeniy Dushistov *
251b71034e5SEvgeniy Dushistov * finds an entry in the specified directory with the wanted name. It
252b71034e5SEvgeniy Dushistov * returns the page in which the entry was found, and the entry itself
253b71034e5SEvgeniy Dushistov * (as a parameter - res_dir). Page is returned mapped and unlocked.
254b71034e5SEvgeniy Dushistov * Entry is guaranteed to be valid.
255b71034e5SEvgeniy Dushistov */
ufs_find_entry(struct inode * dir,const struct qstr * qstr,struct page ** res_page)25689031bc7SAl Viro struct ufs_dir_entry *ufs_find_entry(struct inode *dir, const struct qstr *qstr,
257b71034e5SEvgeniy Dushistov struct page **res_page)
258b71034e5SEvgeniy Dushistov {
259b71034e5SEvgeniy Dushistov struct super_block *sb = dir->i_sb;
26089031bc7SAl Viro const unsigned char *name = qstr->name;
26108049707SAlexey Dobriyan int namelen = qstr->len;
262b71034e5SEvgeniy Dushistov unsigned reclen = UFS_DIR_REC_LEN(namelen);
263b71034e5SEvgeniy Dushistov unsigned long start, n;
2645d754cedSFabian Frederick unsigned long npages = dir_pages(dir);
265b71034e5SEvgeniy Dushistov struct page *page = NULL;
266dd187a26SEvgeniy Dushistov struct ufs_inode_info *ui = UFS_I(dir);
267b71034e5SEvgeniy Dushistov struct ufs_dir_entry *de;
268b71034e5SEvgeniy Dushistov
269abf5d15fSEvgeniy Dushistov UFSD("ENTER, dir_ino %lu, name %s, namlen %u\n", dir->i_ino, name, namelen);
270b71034e5SEvgeniy Dushistov
271b71034e5SEvgeniy Dushistov if (npages == 0 || namelen > UFS_MAXNAMLEN)
272b71034e5SEvgeniy Dushistov goto out;
273b71034e5SEvgeniy Dushistov
274b71034e5SEvgeniy Dushistov /* OFFSET_CACHE */
275b71034e5SEvgeniy Dushistov *res_page = NULL;
276b71034e5SEvgeniy Dushistov
277dd187a26SEvgeniy Dushistov start = ui->i_dir_start_lookup;
278dd187a26SEvgeniy Dushistov
279b71034e5SEvgeniy Dushistov if (start >= npages)
280b71034e5SEvgeniy Dushistov start = 0;
281b71034e5SEvgeniy Dushistov n = start;
282b71034e5SEvgeniy Dushistov do {
283b71034e5SEvgeniy Dushistov char *kaddr;
284b71034e5SEvgeniy Dushistov page = ufs_get_page(dir, n);
285b71034e5SEvgeniy Dushistov if (!IS_ERR(page)) {
286b71034e5SEvgeniy Dushistov kaddr = page_address(page);
287b71034e5SEvgeniy Dushistov de = (struct ufs_dir_entry *) kaddr;
288b71034e5SEvgeniy Dushistov kaddr += ufs_last_byte(dir, n) - reclen;
289b71034e5SEvgeniy Dushistov while ((char *) de <= kaddr) {
290b71034e5SEvgeniy Dushistov if (ufs_match(sb, namelen, name, de))
291b71034e5SEvgeniy Dushistov goto found;
292b71034e5SEvgeniy Dushistov de = ufs_next_entry(sb, de);
293b71034e5SEvgeniy Dushistov }
294b71034e5SEvgeniy Dushistov ufs_put_page(page);
295b71034e5SEvgeniy Dushistov }
296b71034e5SEvgeniy Dushistov if (++n >= npages)
297b71034e5SEvgeniy Dushistov n = 0;
298b71034e5SEvgeniy Dushistov } while (n != start);
299b71034e5SEvgeniy Dushistov out:
300b71034e5SEvgeniy Dushistov return NULL;
301b71034e5SEvgeniy Dushistov
302b71034e5SEvgeniy Dushistov found:
303b71034e5SEvgeniy Dushistov *res_page = page;
304dd187a26SEvgeniy Dushistov ui->i_dir_start_lookup = n;
305b71034e5SEvgeniy Dushistov return de;
306b71034e5SEvgeniy Dushistov }
307b71034e5SEvgeniy Dushistov
308b71034e5SEvgeniy Dushistov /*
309b71034e5SEvgeniy Dushistov * Parent is locked.
310b71034e5SEvgeniy Dushistov */
ufs_add_link(struct dentry * dentry,struct inode * inode)311b71034e5SEvgeniy Dushistov int ufs_add_link(struct dentry *dentry, struct inode *inode)
312b71034e5SEvgeniy Dushistov {
3132b0143b5SDavid Howells struct inode *dir = d_inode(dentry->d_parent);
31489031bc7SAl Viro const unsigned char *name = dentry->d_name.name;
315b71034e5SEvgeniy Dushistov int namelen = dentry->d_name.len;
316b71034e5SEvgeniy Dushistov struct super_block *sb = dir->i_sb;
317b71034e5SEvgeniy Dushistov unsigned reclen = UFS_DIR_REC_LEN(namelen);
318f336953bSEvgeniy Dushistov const unsigned int chunk_size = UFS_SB(sb)->s_uspi->s_dirblksize;
319b71034e5SEvgeniy Dushistov unsigned short rec_len, name_len;
320b71034e5SEvgeniy Dushistov struct page *page = NULL;
321b71034e5SEvgeniy Dushistov struct ufs_dir_entry *de;
3225d754cedSFabian Frederick unsigned long npages = dir_pages(dir);
323b71034e5SEvgeniy Dushistov unsigned long n;
324b71034e5SEvgeniy Dushistov char *kaddr;
32582b9d1d0SNick Piggin loff_t pos;
326b71034e5SEvgeniy Dushistov int err;
327b71034e5SEvgeniy Dushistov
328abf5d15fSEvgeniy Dushistov UFSD("ENTER, name %s, namelen %u\n", name, namelen);
329b71034e5SEvgeniy Dushistov
330b71034e5SEvgeniy Dushistov /*
331b71034e5SEvgeniy Dushistov * We take care of directory expansion in the same loop.
332b71034e5SEvgeniy Dushistov * This code plays outside i_size, so it locks the page
333b71034e5SEvgeniy Dushistov * to protect that region.
334b71034e5SEvgeniy Dushistov */
335b71034e5SEvgeniy Dushistov for (n = 0; n <= npages; n++) {
336b71034e5SEvgeniy Dushistov char *dir_end;
337b71034e5SEvgeniy Dushistov
338b71034e5SEvgeniy Dushistov page = ufs_get_page(dir, n);
339b71034e5SEvgeniy Dushistov err = PTR_ERR(page);
340b71034e5SEvgeniy Dushistov if (IS_ERR(page))
341b71034e5SEvgeniy Dushistov goto out;
342b71034e5SEvgeniy Dushistov lock_page(page);
343b71034e5SEvgeniy Dushistov kaddr = page_address(page);
344b71034e5SEvgeniy Dushistov dir_end = kaddr + ufs_last_byte(dir, n);
345b71034e5SEvgeniy Dushistov de = (struct ufs_dir_entry *)kaddr;
34609cbfeafSKirill A. Shutemov kaddr += PAGE_SIZE - reclen;
347b71034e5SEvgeniy Dushistov while ((char *)de <= kaddr) {
348b71034e5SEvgeniy Dushistov if ((char *)de == dir_end) {
349b71034e5SEvgeniy Dushistov /* We hit i_size */
350b71034e5SEvgeniy Dushistov name_len = 0;
351f336953bSEvgeniy Dushistov rec_len = chunk_size;
352f336953bSEvgeniy Dushistov de->d_reclen = cpu_to_fs16(sb, chunk_size);
353b71034e5SEvgeniy Dushistov de->d_ino = 0;
354b71034e5SEvgeniy Dushistov goto got_it;
355b71034e5SEvgeniy Dushistov }
356b71034e5SEvgeniy Dushistov if (de->d_reclen == 0) {
3579746077aSHarvey Harrison ufs_error(dir->i_sb, __func__,
358b71034e5SEvgeniy Dushistov "zero-length directory entry");
359b71034e5SEvgeniy Dushistov err = -EIO;
360b71034e5SEvgeniy Dushistov goto out_unlock;
361b71034e5SEvgeniy Dushistov }
362b71034e5SEvgeniy Dushistov err = -EEXIST;
363b71034e5SEvgeniy Dushistov if (ufs_match(sb, namelen, name, de))
364b71034e5SEvgeniy Dushistov goto out_unlock;
365b71034e5SEvgeniy Dushistov name_len = UFS_DIR_REC_LEN(ufs_get_de_namlen(sb, de));
366b71034e5SEvgeniy Dushistov rec_len = fs16_to_cpu(sb, de->d_reclen);
367b71034e5SEvgeniy Dushistov if (!de->d_ino && rec_len >= reclen)
368b71034e5SEvgeniy Dushistov goto got_it;
369b71034e5SEvgeniy Dushistov if (rec_len >= name_len + reclen)
370b71034e5SEvgeniy Dushistov goto got_it;
371b71034e5SEvgeniy Dushistov de = (struct ufs_dir_entry *) ((char *) de + rec_len);
372b71034e5SEvgeniy Dushistov }
373b71034e5SEvgeniy Dushistov unlock_page(page);
374b71034e5SEvgeniy Dushistov ufs_put_page(page);
375b71034e5SEvgeniy Dushistov }
376b71034e5SEvgeniy Dushistov BUG();
377b71034e5SEvgeniy Dushistov return -EINVAL;
378b71034e5SEvgeniy Dushistov
379b71034e5SEvgeniy Dushistov got_it:
38082b9d1d0SNick Piggin pos = page_offset(page) +
38182b9d1d0SNick Piggin (char*)de - (char*)page_address(page);
382f4e420dcSChristoph Hellwig err = ufs_prepare_chunk(page, pos, rec_len);
383b71034e5SEvgeniy Dushistov if (err)
384b71034e5SEvgeniy Dushistov goto out_unlock;
385b71034e5SEvgeniy Dushistov if (de->d_ino) {
386b71034e5SEvgeniy Dushistov struct ufs_dir_entry *de1 =
387b71034e5SEvgeniy Dushistov (struct ufs_dir_entry *) ((char *) de + name_len);
388b71034e5SEvgeniy Dushistov de1->d_reclen = cpu_to_fs16(sb, rec_len - name_len);
389b71034e5SEvgeniy Dushistov de->d_reclen = cpu_to_fs16(sb, name_len);
390b71034e5SEvgeniy Dushistov
391b71034e5SEvgeniy Dushistov de = de1;
392b71034e5SEvgeniy Dushistov }
393b71034e5SEvgeniy Dushistov
394b71034e5SEvgeniy Dushistov ufs_set_de_namlen(sb, de, namelen);
395b71034e5SEvgeniy Dushistov memcpy(de->d_name, name, namelen + 1);
396b71034e5SEvgeniy Dushistov de->d_ino = cpu_to_fs32(sb, inode->i_ino);
397b71034e5SEvgeniy Dushistov ufs_set_de_type(sb, de, inode->i_mode);
398b71034e5SEvgeniy Dushistov
3999e22031aSChristoph Hellwig ufs_commit_chunk(page, pos, rec_len);
400*6eeb017eSJeff Layton dir->i_mtime = inode_set_ctime_current(dir);
401b71034e5SEvgeniy Dushistov
402b71034e5SEvgeniy Dushistov mark_inode_dirty(dir);
4039e22031aSChristoph Hellwig err = ufs_handle_dirsync(dir);
404b71034e5SEvgeniy Dushistov /* OFFSET_CACHE */
405b71034e5SEvgeniy Dushistov out_put:
406b71034e5SEvgeniy Dushistov ufs_put_page(page);
407b71034e5SEvgeniy Dushistov out:
408b71034e5SEvgeniy Dushistov return err;
409b71034e5SEvgeniy Dushistov out_unlock:
410b71034e5SEvgeniy Dushistov unlock_page(page);
411b71034e5SEvgeniy Dushistov goto out_put;
412b71034e5SEvgeniy Dushistov }
413b71034e5SEvgeniy Dushistov
414b71034e5SEvgeniy Dushistov static inline unsigned
ufs_validate_entry(struct super_block * sb,char * base,unsigned offset,unsigned mask)415b71034e5SEvgeniy Dushistov ufs_validate_entry(struct super_block *sb, char *base,
416b71034e5SEvgeniy Dushistov unsigned offset, unsigned mask)
417b71034e5SEvgeniy Dushistov {
418b71034e5SEvgeniy Dushistov struct ufs_dir_entry *de = (struct ufs_dir_entry*)(base + offset);
419b71034e5SEvgeniy Dushistov struct ufs_dir_entry *p = (struct ufs_dir_entry*)(base + (offset&mask));
420e0d508f1SAl Viro while ((char*)p < (char*)de)
421b71034e5SEvgeniy Dushistov p = ufs_next_entry(sb, p);
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
ufs_readdir(struct file * file,struct dir_context * ctx)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);
439c472c07bSGoffredo Baroncelli bool need_revalidate = !inode_eq_iversion(inode, file->f_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 }
466bb8c2d66SJeff Layton file->f_version = inode_query_iversion(inode);
467bb8c2d66SJeff Layton need_revalidate = false;
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)) {
4721da177e4SLinus Torvalds if (de->d_ino) {
4731da177e4SLinus Torvalds unsigned char d_type = DT_UNKNOWN;
4741da177e4SLinus Torvalds
475abf5d15fSEvgeniy Dushistov UFSD("filldir(%s,%u)\n", de->d_name,
476abf5d15fSEvgeniy Dushistov fs32_to_cpu(sb, de->d_ino));
477abf5d15fSEvgeniy Dushistov UFSD("namlen %u\n", ufs_get_de_namlen(sb, de));
4781da177e4SLinus Torvalds
4791da177e4SLinus Torvalds if ((flags & UFS_DE_MASK) == UFS_DE_44BSD)
4801da177e4SLinus Torvalds d_type = de->d_u.d_44.d_type;
481b71034e5SEvgeniy Dushistov
48280886298SAl Viro if (!dir_emit(ctx, de->d_name,
483b71034e5SEvgeniy Dushistov ufs_get_de_namlen(sb, de),
48480886298SAl Viro fs32_to_cpu(sb, de->d_ino),
48580886298SAl Viro d_type)) {
486b71034e5SEvgeniy Dushistov ufs_put_page(page);
487b71034e5SEvgeniy Dushistov return 0;
488b71034e5SEvgeniy Dushistov }
4891da177e4SLinus Torvalds }
49080886298SAl Viro ctx->pos += fs16_to_cpu(sb, de->d_reclen);
4911da177e4SLinus Torvalds }
492b71034e5SEvgeniy Dushistov ufs_put_page(page);
4931da177e4SLinus Torvalds }
4941da177e4SLinus Torvalds return 0;
4951da177e4SLinus Torvalds }
4961da177e4SLinus Torvalds
4971da177e4SLinus Torvalds
4981da177e4SLinus Torvalds /*
4991da177e4SLinus Torvalds * ufs_delete_entry deletes a directory entry by merging it with the
5001da177e4SLinus Torvalds * previous entry.
5011da177e4SLinus Torvalds */
ufs_delete_entry(struct inode * inode,struct ufs_dir_entry * dir,struct page * page)5021da177e4SLinus Torvalds int ufs_delete_entry(struct inode *inode, struct ufs_dir_entry *dir,
503b71034e5SEvgeniy Dushistov struct page * page)
5041da177e4SLinus Torvalds {
505b71034e5SEvgeniy Dushistov struct super_block *sb = inode->i_sb;
506b71034e5SEvgeniy Dushistov char *kaddr = page_address(page);
507f336953bSEvgeniy Dushistov unsigned from = ((char*)dir - kaddr) & ~(UFS_SB(sb)->s_uspi->s_dirblksize - 1);
508b71034e5SEvgeniy Dushistov unsigned to = ((char*)dir - kaddr) + fs16_to_cpu(sb, dir->d_reclen);
50982b9d1d0SNick Piggin loff_t pos;
510b71034e5SEvgeniy Dushistov struct ufs_dir_entry *pde = NULL;
511b71034e5SEvgeniy Dushistov struct ufs_dir_entry *de = (struct ufs_dir_entry *) (kaddr + from);
512b71034e5SEvgeniy Dushistov int err;
5131da177e4SLinus Torvalds
514abf5d15fSEvgeniy Dushistov UFSD("ENTER\n");
5151da177e4SLinus Torvalds
516abf5d15fSEvgeniy Dushistov UFSD("ino %u, reclen %u, namlen %u, name %s\n",
5171da177e4SLinus Torvalds fs32_to_cpu(sb, de->d_ino),
518221fc10eSEvgeniy fs16_to_cpu(sb, de->d_reclen),
519abf5d15fSEvgeniy Dushistov ufs_get_de_namlen(sb, de), de->d_name);
5201da177e4SLinus Torvalds
521b71034e5SEvgeniy Dushistov while ((char*)de < (char*)dir) {
522b71034e5SEvgeniy Dushistov if (de->d_reclen == 0) {
5239746077aSHarvey Harrison ufs_error(inode->i_sb, __func__,
524b71034e5SEvgeniy Dushistov "zero-length directory entry");
525b71034e5SEvgeniy Dushistov err = -EIO;
526b71034e5SEvgeniy Dushistov goto out;
5271da177e4SLinus Torvalds }
528b71034e5SEvgeniy Dushistov pde = de;
529b71034e5SEvgeniy Dushistov de = ufs_next_entry(sb, de);
530b71034e5SEvgeniy Dushistov }
5311da177e4SLinus Torvalds if (pde)
532b71034e5SEvgeniy Dushistov from = (char*)pde - (char*)page_address(page);
53382b9d1d0SNick Piggin
53482b9d1d0SNick Piggin pos = page_offset(page) + from;
535b71034e5SEvgeniy Dushistov lock_page(page);
536f4e420dcSChristoph Hellwig err = ufs_prepare_chunk(page, pos, to - from);
537b71034e5SEvgeniy Dushistov BUG_ON(err);
538b71034e5SEvgeniy Dushistov if (pde)
539b71034e5SEvgeniy Dushistov pde->d_reclen = cpu_to_fs16(sb, to - from);
5401da177e4SLinus Torvalds dir->d_ino = 0;
5419e22031aSChristoph Hellwig ufs_commit_chunk(page, pos, to - from);
542*6eeb017eSJeff Layton inode->i_mtime = inode_set_ctime_current(inode);
5431da177e4SLinus Torvalds mark_inode_dirty(inode);
5449e22031aSChristoph Hellwig err = ufs_handle_dirsync(inode);
545b71034e5SEvgeniy Dushistov out:
546b71034e5SEvgeniy Dushistov ufs_put_page(page);
547abf5d15fSEvgeniy Dushistov UFSD("EXIT\n");
548b71034e5SEvgeniy Dushistov return err;
5491da177e4SLinus Torvalds }
5501da177e4SLinus Torvalds
ufs_make_empty(struct inode * inode,struct inode * dir)5511da177e4SLinus Torvalds int ufs_make_empty(struct inode * inode, struct inode *dir)
5521da177e4SLinus Torvalds {
5531da177e4SLinus Torvalds struct super_block * sb = dir->i_sb;
554b71034e5SEvgeniy Dushistov struct address_space *mapping = inode->i_mapping;
555b71034e5SEvgeniy Dushistov struct page *page = grab_cache_page(mapping, 0);
556f336953bSEvgeniy Dushistov const unsigned int chunk_size = UFS_SB(sb)->s_uspi->s_dirblksize;
5571da177e4SLinus Torvalds struct ufs_dir_entry * de;
558b71034e5SEvgeniy Dushistov char *base;
5591da177e4SLinus Torvalds int err;
5601da177e4SLinus Torvalds
561b71034e5SEvgeniy Dushistov if (!page)
562b71034e5SEvgeniy Dushistov return -ENOMEM;
56382b9d1d0SNick Piggin
564f4e420dcSChristoph Hellwig err = ufs_prepare_chunk(page, 0, chunk_size);
565b71034e5SEvgeniy Dushistov if (err) {
566b71034e5SEvgeniy Dushistov unlock_page(page);
567b71034e5SEvgeniy Dushistov goto fail;
568b71034e5SEvgeniy Dushistov }
5691da177e4SLinus Torvalds
57082b9d1d0SNick Piggin kmap(page);
571b71034e5SEvgeniy Dushistov base = (char*)page_address(page);
57209cbfeafSKirill A. Shutemov memset(base, 0, PAGE_SIZE);
573b71034e5SEvgeniy Dushistov
574b71034e5SEvgeniy Dushistov de = (struct ufs_dir_entry *) base;
575b71034e5SEvgeniy Dushistov
5761da177e4SLinus Torvalds de->d_ino = cpu_to_fs32(sb, inode->i_ino);
5771da177e4SLinus Torvalds ufs_set_de_type(sb, de, inode->i_mode);
5781da177e4SLinus Torvalds ufs_set_de_namlen(sb, de, 1);
5791da177e4SLinus Torvalds de->d_reclen = cpu_to_fs16(sb, UFS_DIR_REC_LEN(1));
5801da177e4SLinus Torvalds strcpy (de->d_name, ".");
5811da177e4SLinus Torvalds de = (struct ufs_dir_entry *)
5821da177e4SLinus Torvalds ((char *)de + fs16_to_cpu(sb, de->d_reclen));
5831da177e4SLinus Torvalds de->d_ino = cpu_to_fs32(sb, dir->i_ino);
5841da177e4SLinus Torvalds ufs_set_de_type(sb, de, dir->i_mode);
585f336953bSEvgeniy Dushistov de->d_reclen = cpu_to_fs16(sb, chunk_size - UFS_DIR_REC_LEN(1));
5861da177e4SLinus Torvalds ufs_set_de_namlen(sb, de, 2);
5871da177e4SLinus Torvalds strcpy (de->d_name, "..");
58882b9d1d0SNick Piggin kunmap(page);
589b71034e5SEvgeniy Dushistov
5909e22031aSChristoph Hellwig ufs_commit_chunk(page, 0, chunk_size);
5919e22031aSChristoph Hellwig err = ufs_handle_dirsync(inode);
592b71034e5SEvgeniy Dushistov fail:
59309cbfeafSKirill A. Shutemov put_page(page);
594b71034e5SEvgeniy Dushistov return err;
5951da177e4SLinus Torvalds }
5961da177e4SLinus Torvalds
5971da177e4SLinus Torvalds /*
5981da177e4SLinus Torvalds * routine to check that the specified directory is empty (for rmdir)
5991da177e4SLinus Torvalds */
ufs_empty_dir(struct inode * inode)6001da177e4SLinus Torvalds int ufs_empty_dir(struct inode * inode)
6011da177e4SLinus Torvalds {
602b71034e5SEvgeniy Dushistov struct super_block *sb = inode->i_sb;
603b71034e5SEvgeniy Dushistov struct page *page = NULL;
6045d754cedSFabian Frederick unsigned long i, npages = dir_pages(inode);
6051da177e4SLinus Torvalds
606b71034e5SEvgeniy Dushistov for (i = 0; i < npages; i++) {
607b71034e5SEvgeniy Dushistov char *kaddr;
608b71034e5SEvgeniy Dushistov struct ufs_dir_entry *de;
609b71034e5SEvgeniy Dushistov page = ufs_get_page(inode, i);
6101da177e4SLinus Torvalds
611b71034e5SEvgeniy Dushistov if (IS_ERR(page))
6121da177e4SLinus Torvalds continue;
613b71034e5SEvgeniy Dushistov
614b71034e5SEvgeniy Dushistov kaddr = page_address(page);
615b71034e5SEvgeniy Dushistov de = (struct ufs_dir_entry *)kaddr;
616b71034e5SEvgeniy Dushistov kaddr += ufs_last_byte(inode, i) - UFS_DIR_REC_LEN(1);
617b71034e5SEvgeniy Dushistov
618b71034e5SEvgeniy Dushistov while ((char *)de <= kaddr) {
619b71034e5SEvgeniy Dushistov if (de->d_reclen == 0) {
6209746077aSHarvey Harrison ufs_error(inode->i_sb, __func__,
621b71034e5SEvgeniy Dushistov "zero-length directory entry: "
622b71034e5SEvgeniy Dushistov "kaddr=%p, de=%p\n", kaddr, de);
623b71034e5SEvgeniy Dushistov goto not_empty;
6241da177e4SLinus Torvalds }
6251da177e4SLinus Torvalds if (de->d_ino) {
626b71034e5SEvgeniy Dushistov u16 namelen=ufs_get_de_namlen(sb, de);
627b71034e5SEvgeniy Dushistov /* check for . and .. */
628b71034e5SEvgeniy Dushistov if (de->d_name[0] != '.')
629b71034e5SEvgeniy Dushistov goto not_empty;
630b71034e5SEvgeniy Dushistov if (namelen > 2)
631b71034e5SEvgeniy Dushistov goto not_empty;
632b71034e5SEvgeniy Dushistov if (namelen < 2) {
633b71034e5SEvgeniy Dushistov if (inode->i_ino !=
634b71034e5SEvgeniy Dushistov fs32_to_cpu(sb, de->d_ino))
635b71034e5SEvgeniy Dushistov goto not_empty;
636b71034e5SEvgeniy Dushistov } else if (de->d_name[1] != '.')
637b71034e5SEvgeniy Dushistov goto not_empty;
6381da177e4SLinus Torvalds }
639b71034e5SEvgeniy Dushistov de = ufs_next_entry(sb, de);
6401da177e4SLinus Torvalds }
641b71034e5SEvgeniy Dushistov ufs_put_page(page);
642b71034e5SEvgeniy Dushistov }
6431da177e4SLinus Torvalds return 1;
644b71034e5SEvgeniy Dushistov
645b71034e5SEvgeniy Dushistov not_empty:
646b71034e5SEvgeniy Dushistov ufs_put_page(page);
647b71034e5SEvgeniy Dushistov return 0;
6481da177e4SLinus Torvalds }
6491da177e4SLinus Torvalds
6504b6f5d20SArjan van de Ven const struct file_operations ufs_dir_operations = {
6511da177e4SLinus Torvalds .read = generic_read_dir,
6523b0a3c1aSAl Viro .iterate_shared = ufs_readdir,
6531b061d92SChristoph Hellwig .fsync = generic_file_fsync,
6543222a3e5SChristoph Hellwig .llseek = generic_file_llseek,
6551da177e4SLinus Torvalds };
656