Linux Audio

Check our new training course

Loading...
v5.9
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
  4 */
  5
  6#include <linux/init.h>
  7#include <linux/buffer_head.h>
  8#include <linux/mpage.h>
  9#include <linux/bio.h>
 10#include <linux/blkdev.h>
 11#include <linux/time.h>
 12#include <linux/writeback.h>
 13#include <linux/uio.h>
 14#include <linux/random.h>
 15#include <linux/iversion.h>
 16
 17#include "exfat_raw.h"
 18#include "exfat_fs.h"
 19
 20static int __exfat_write_inode(struct inode *inode, int sync)
 21{
 22	unsigned long long on_disk_size;
 23	struct exfat_dentry *ep, *ep2;
 24	struct exfat_entry_set_cache *es = NULL;
 25	struct super_block *sb = inode->i_sb;
 26	struct exfat_sb_info *sbi = EXFAT_SB(sb);
 27	struct exfat_inode_info *ei = EXFAT_I(inode);
 28	bool is_dir = (ei->type == TYPE_DIR) ? true : false;
 
 29
 30	if (inode->i_ino == EXFAT_ROOT_INO)
 31		return 0;
 32
 33	/*
 34	 * If the indode is already unlinked, there is no need for updating it.
 35	 */
 36	if (ei->dir.dir == DIR_DELETED)
 37		return 0;
 38
 39	if (is_dir && ei->dir.dir == sbi->root_dir && ei->entry == -1)
 40		return 0;
 41
 42	exfat_set_volume_dirty(sb);
 43
 44	/* get the directory entry of given file or directory */
 45	es = exfat_get_dentry_set(sb, &(ei->dir), ei->entry, ES_ALL_ENTRIES);
 46	if (!es)
 47		return -EIO;
 48	ep = exfat_get_dentry_cached(es, 0);
 49	ep2 = exfat_get_dentry_cached(es, 1);
 50
 51	ep->dentry.file.attr = cpu_to_le16(exfat_make_attr(inode));
 52
 53	/* set FILE_INFO structure using the acquired struct exfat_dentry */
 54	exfat_set_entry_time(sbi, &ei->i_crtime,
 55			&ep->dentry.file.create_tz,
 56			&ep->dentry.file.create_time,
 57			&ep->dentry.file.create_date,
 58			&ep->dentry.file.create_time_cs);
 59	exfat_set_entry_time(sbi, &inode->i_mtime,
 60			&ep->dentry.file.modify_tz,
 61			&ep->dentry.file.modify_time,
 62			&ep->dentry.file.modify_date,
 63			&ep->dentry.file.modify_time_cs);
 64	exfat_set_entry_time(sbi, &inode->i_atime,
 65			&ep->dentry.file.access_tz,
 66			&ep->dentry.file.access_time,
 67			&ep->dentry.file.access_date,
 68			NULL);
 
 
 69
 70	/* File size should be zero if there is no cluster allocated */
 71	on_disk_size = i_size_read(inode);
 72
 73	if (ei->start_clu == EXFAT_EOF_CLUSTER)
 74		on_disk_size = 0;
 75
 76	ep2->dentry.stream.valid_size = cpu_to_le64(on_disk_size);
 77	ep2->dentry.stream.size = ep2->dentry.stream.valid_size;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 78
 79	exfat_update_dir_chksum_with_entry_set(es);
 80	return exfat_free_dentry_set(es, sync);
 81}
 82
 83int exfat_write_inode(struct inode *inode, struct writeback_control *wbc)
 84{
 85	int ret;
 86
 
 
 
 87	mutex_lock(&EXFAT_SB(inode->i_sb)->s_lock);
 88	ret = __exfat_write_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
 89	mutex_unlock(&EXFAT_SB(inode->i_sb)->s_lock);
 90
 91	return ret;
 92}
 93
 94void exfat_sync_inode(struct inode *inode)
 95{
 96	lockdep_assert_held(&EXFAT_SB(inode->i_sb)->s_lock);
 97	__exfat_write_inode(inode, 1);
 98}
 99
100/*
101 * Input: inode, (logical) clu_offset, target allocation area
102 * Output: errcode, cluster number
103 * *clu = (~0), if it's unable to allocate a new cluster
104 */
105static int exfat_map_cluster(struct inode *inode, unsigned int clu_offset,
106		unsigned int *clu, int create)
107{
108	int ret, modified = false;
109	unsigned int last_clu;
110	struct exfat_chain new_clu;
111	struct super_block *sb = inode->i_sb;
112	struct exfat_sb_info *sbi = EXFAT_SB(sb);
113	struct exfat_inode_info *ei = EXFAT_I(inode);
114	unsigned int local_clu_offset = clu_offset;
115	unsigned int num_to_be_allocated = 0, num_clusters = 0;
116
117	ei->rwoffset = EXFAT_CLU_TO_B(clu_offset, sbi);
118
119	if (EXFAT_I(inode)->i_size_ondisk > 0)
120		num_clusters =
121			EXFAT_B_TO_CLU_ROUND_UP(EXFAT_I(inode)->i_size_ondisk,
122			sbi);
123
124	if (clu_offset >= num_clusters)
125		num_to_be_allocated = clu_offset - num_clusters + 1;
126
127	if (!create && (num_to_be_allocated > 0)) {
128		*clu = EXFAT_EOF_CLUSTER;
129		return 0;
130	}
131
132	*clu = last_clu = ei->start_clu;
133
134	if (ei->flags == ALLOC_NO_FAT_CHAIN) {
135		if (clu_offset > 0 && *clu != EXFAT_EOF_CLUSTER) {
136			last_clu += clu_offset - 1;
137
138			if (clu_offset == num_clusters)
139				*clu = EXFAT_EOF_CLUSTER;
140			else
141				*clu += clu_offset;
142		}
143	} else if (ei->type == TYPE_FILE) {
144		unsigned int fclus = 0;
145		int err = exfat_get_cluster(inode, clu_offset,
146				&fclus, clu, &last_clu, 1);
147		if (err)
148			return -EIO;
149
150		clu_offset -= fclus;
151	} else {
152		/* hint information */
153		if (clu_offset > 0 && ei->hint_bmap.off != EXFAT_EOF_CLUSTER &&
154		    ei->hint_bmap.off > 0 && clu_offset >= ei->hint_bmap.off) {
155			clu_offset -= ei->hint_bmap.off;
156			/* hint_bmap.clu should be valid */
157			WARN_ON(ei->hint_bmap.clu < 2);
158			*clu = ei->hint_bmap.clu;
159		}
160
161		while (clu_offset > 0 && *clu != EXFAT_EOF_CLUSTER) {
162			last_clu = *clu;
163			if (exfat_get_next_cluster(sb, clu))
164				return -EIO;
165			clu_offset--;
166		}
167	}
168
169	if (*clu == EXFAT_EOF_CLUSTER) {
170		exfat_set_volume_dirty(sb);
171
172		new_clu.dir = (last_clu == EXFAT_EOF_CLUSTER) ?
173				EXFAT_EOF_CLUSTER : last_clu + 1;
174		new_clu.size = 0;
175		new_clu.flags = ei->flags;
176
177		/* allocate a cluster */
178		if (num_to_be_allocated < 1) {
179			/* Broken FAT (i_sze > allocated FAT) */
180			exfat_fs_error(sb, "broken FAT chain.");
181			return -EIO;
182		}
183
184		ret = exfat_alloc_cluster(inode, num_to_be_allocated, &new_clu);
 
185		if (ret)
186			return ret;
187
188		if (new_clu.dir == EXFAT_EOF_CLUSTER ||
189		    new_clu.dir == EXFAT_FREE_CLUSTER) {
190			exfat_fs_error(sb,
191				"bogus cluster new allocated (last_clu : %u, new_clu : %u)",
192				last_clu, new_clu.dir);
193			return -EIO;
194		}
195
196		/* append to the FAT chain */
197		if (last_clu == EXFAT_EOF_CLUSTER) {
198			if (new_clu.flags == ALLOC_FAT_CHAIN)
199				ei->flags = ALLOC_FAT_CHAIN;
200			ei->start_clu = new_clu.dir;
201			modified = true;
202		} else {
203			if (new_clu.flags != ei->flags) {
204				/* no-fat-chain bit is disabled,
205				 * so fat-chain should be synced with
206				 * alloc-bitmap
207				 */
208				exfat_chain_cont_cluster(sb, ei->start_clu,
209					num_clusters);
210				ei->flags = ALLOC_FAT_CHAIN;
211				modified = true;
212			}
213			if (new_clu.flags == ALLOC_FAT_CHAIN)
214				if (exfat_ent_set(sb, last_clu, new_clu.dir))
215					return -EIO;
216		}
217
218		num_clusters += num_to_be_allocated;
219		*clu = new_clu.dir;
220
221		if (ei->dir.dir != DIR_DELETED && modified) {
222			struct exfat_dentry *ep;
223			struct exfat_entry_set_cache *es;
224			int err;
225
226			es = exfat_get_dentry_set(sb, &(ei->dir), ei->entry,
227				ES_ALL_ENTRIES);
228			if (!es)
229				return -EIO;
230			/* get stream entry */
231			ep = exfat_get_dentry_cached(es, 1);
232
233			/* update directory entry */
234			ep->dentry.stream.flags = ei->flags;
235			ep->dentry.stream.start_clu =
236				cpu_to_le32(ei->start_clu);
237			ep->dentry.stream.valid_size =
238				cpu_to_le64(i_size_read(inode));
239			ep->dentry.stream.size =
240				ep->dentry.stream.valid_size;
241
242			exfat_update_dir_chksum_with_entry_set(es);
243			err = exfat_free_dentry_set(es, inode_needs_sync(inode));
244			if (err)
245				return err;
246		} /* end of if != DIR_DELETED */
247
248		inode->i_blocks +=
249			num_to_be_allocated << sbi->sect_per_clus_bits;
250
251		/*
252		 * Move *clu pointer along FAT chains (hole care) because the
253		 * caller of this function expect *clu to be the last cluster.
254		 * This only works when num_to_be_allocated >= 2,
255		 * *clu = (the first cluster of the allocated chain) =>
256		 * (the last cluster of ...)
257		 */
258		if (ei->flags == ALLOC_NO_FAT_CHAIN) {
259			*clu += num_to_be_allocated - 1;
260		} else {
261			while (num_to_be_allocated > 1) {
262				if (exfat_get_next_cluster(sb, clu))
263					return -EIO;
264				num_to_be_allocated--;
265			}
266		}
267
268	}
269
270	/* hint information */
271	ei->hint_bmap.off = local_clu_offset;
272	ei->hint_bmap.clu = *clu;
273
274	return 0;
275}
276
277static int exfat_map_new_buffer(struct exfat_inode_info *ei,
278		struct buffer_head *bh, loff_t pos)
279{
280	if (buffer_delay(bh) && pos > ei->i_size_aligned)
281		return -EIO;
282	set_buffer_new(bh);
283
284	/*
285	 * Adjust i_size_aligned if i_size_ondisk is bigger than it.
286	 */
287	if (ei->i_size_ondisk > ei->i_size_aligned)
288		ei->i_size_aligned = ei->i_size_ondisk;
289	return 0;
290}
291
292static int exfat_get_block(struct inode *inode, sector_t iblock,
293		struct buffer_head *bh_result, int create)
294{
295	struct exfat_inode_info *ei = EXFAT_I(inode);
296	struct super_block *sb = inode->i_sb;
297	struct exfat_sb_info *sbi = EXFAT_SB(sb);
298	unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits;
299	int err = 0;
300	unsigned long mapped_blocks = 0;
301	unsigned int cluster, sec_offset;
302	sector_t last_block;
303	sector_t phys = 0;
304	loff_t pos;
305
306	mutex_lock(&sbi->s_lock);
307	last_block = EXFAT_B_TO_BLK_ROUND_UP(i_size_read(inode), sb);
308	if (iblock >= last_block && !create)
309		goto done;
310
311	/* Is this block already allocated? */
312	err = exfat_map_cluster(inode, iblock >> sbi->sect_per_clus_bits,
313			&cluster, create);
314	if (err) {
315		if (err != -ENOSPC)
316			exfat_fs_error_ratelimit(sb,
317				"failed to bmap (inode : %p iblock : %llu, err : %d)",
318				inode, (unsigned long long)iblock, err);
319		goto unlock_ret;
320	}
321
322	if (cluster == EXFAT_EOF_CLUSTER)
323		goto done;
324
325	/* sector offset in cluster */
326	sec_offset = iblock & (sbi->sect_per_clus - 1);
327
328	phys = exfat_cluster_to_sector(sbi, cluster) + sec_offset;
329	mapped_blocks = sbi->sect_per_clus - sec_offset;
330	max_blocks = min(mapped_blocks, max_blocks);
331
332	/* Treat newly added block / cluster */
333	if (iblock < last_block)
334		create = 0;
335
336	if (create || buffer_delay(bh_result)) {
337		pos = EXFAT_BLK_TO_B((iblock + 1), sb);
338		if (ei->i_size_ondisk < pos)
339			ei->i_size_ondisk = pos;
340	}
341
342	if (create) {
343		err = exfat_map_new_buffer(ei, bh_result, pos);
344		if (err) {
345			exfat_fs_error(sb,
346					"requested for bmap out of range(pos : (%llu) > i_size_aligned(%llu)\n",
347					pos, ei->i_size_aligned);
348			goto unlock_ret;
 
 
 
 
 
 
349		}
350	}
351
352	if (buffer_delay(bh_result))
353		clear_buffer_delay(bh_result);
354	map_bh(bh_result, sb, phys);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
355done:
356	bh_result->b_size = EXFAT_BLK_TO_B(max_blocks, sb);
357unlock_ret:
358	mutex_unlock(&sbi->s_lock);
359	return err;
360}
361
362static int exfat_readpage(struct file *file, struct page *page)
363{
364	return mpage_readpage(page, exfat_get_block);
365}
366
367static void exfat_readahead(struct readahead_control *rac)
368{
369	mpage_readahead(rac, exfat_get_block);
370}
 
 
371
372static int exfat_writepage(struct page *page, struct writeback_control *wbc)
373{
374	return block_write_full_page(page, exfat_get_block, wbc);
 
 
 
 
375}
376
377static int exfat_writepages(struct address_space *mapping,
378		struct writeback_control *wbc)
379{
 
 
 
380	return mpage_writepages(mapping, wbc, exfat_get_block);
381}
382
383static void exfat_write_failed(struct address_space *mapping, loff_t to)
384{
385	struct inode *inode = mapping->host;
386
387	if (to > i_size_read(inode)) {
388		truncate_pagecache(inode, i_size_read(inode));
389		exfat_truncate(inode, EXFAT_I(inode)->i_size_aligned);
 
390	}
391}
392
393static int exfat_write_begin(struct file *file, struct address_space *mapping,
394		loff_t pos, unsigned int len, unsigned int flags,
395		struct page **pagep, void **fsdata)
396{
397	int ret;
398
399	*pagep = NULL;
400	ret = cont_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
401			       exfat_get_block,
402			       &EXFAT_I(mapping->host)->i_size_ondisk);
403
404	if (ret < 0)
405		exfat_write_failed(mapping, pos+len);
406
407	return ret;
408}
409
410static int exfat_write_end(struct file *file, struct address_space *mapping,
411		loff_t pos, unsigned int len, unsigned int copied,
412		struct page *pagep, void *fsdata)
413{
414	struct inode *inode = mapping->host;
415	struct exfat_inode_info *ei = EXFAT_I(inode);
416	int err;
417
418	err = generic_write_end(file, mapping, pos, len, copied, pagep, fsdata);
419
420	if (EXFAT_I(inode)->i_size_aligned < i_size_read(inode)) {
421		exfat_fs_error(inode->i_sb,
422			"invalid size(size(%llu) > aligned(%llu)\n",
423			i_size_read(inode), EXFAT_I(inode)->i_size_aligned);
424		return -EIO;
425	}
426
427	if (err < len)
428		exfat_write_failed(mapping, pos+len);
429
430	if (!(err < 0) && !(ei->attr & ATTR_ARCHIVE)) {
431		inode->i_mtime = inode->i_ctime = current_time(inode);
432		ei->attr |= ATTR_ARCHIVE;
 
 
 
 
 
433		mark_inode_dirty(inode);
434	}
435
436	return err;
437}
438
439static ssize_t exfat_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
440{
441	struct address_space *mapping = iocb->ki_filp->f_mapping;
442	struct inode *inode = mapping->host;
443	loff_t size = iocb->ki_pos + iov_iter_count(iter);
 
 
444	int rw = iov_iter_rw(iter);
445	ssize_t ret;
446
447	if (rw == WRITE) {
448		/*
449		 * FIXME: blockdev_direct_IO() doesn't use ->write_begin(),
450		 * so we need to update the ->i_size_aligned to block boundary.
451		 *
452		 * But we must fill the remaining area or hole by nul for
453		 * updating ->i_size_aligned
454		 *
455		 * Return 0, and fallback to normal buffered write.
456		 */
457		if (EXFAT_I(inode)->i_size_aligned < size)
458			return 0;
459	}
460
461	/*
462	 * Need to use the DIO_LOCKING for avoiding the race
463	 * condition of exfat_get_block() and ->truncate().
464	 */
465	ret = blockdev_direct_IO(iocb, inode, iter, exfat_get_block);
466	if (ret < 0 && (rw & WRITE))
467		exfat_write_failed(mapping, size);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
468	return ret;
469}
470
471static sector_t exfat_aop_bmap(struct address_space *mapping, sector_t block)
472{
473	sector_t blocknr;
474
475	/* exfat_get_cluster() assumes the requested blocknr isn't truncated. */
476	down_read(&EXFAT_I(mapping->host)->truncate_lock);
477	blocknr = generic_block_bmap(mapping, block, exfat_get_block);
478	up_read(&EXFAT_I(mapping->host)->truncate_lock);
479	return blocknr;
480}
481
482/*
483 * exfat_block_truncate_page() zeroes out a mapping from file offset `from'
484 * up to the end of the block which corresponds to `from'.
485 * This is required during truncate to physically zeroout the tail end
486 * of that block so it doesn't yield old data if the file is later grown.
487 * Also, avoid causing failure from fsx for cases of "data past EOF"
488 */
489int exfat_block_truncate_page(struct inode *inode, loff_t from)
490{
491	return block_truncate_page(inode->i_mapping, from, exfat_get_block);
492}
493
494static const struct address_space_operations exfat_aops = {
495	.readpage	= exfat_readpage,
 
 
496	.readahead	= exfat_readahead,
497	.writepage	= exfat_writepage,
498	.writepages	= exfat_writepages,
499	.write_begin	= exfat_write_begin,
500	.write_end	= exfat_write_end,
501	.direct_IO	= exfat_direct_IO,
502	.bmap		= exfat_aop_bmap
 
503};
504
505static inline unsigned long exfat_hash(loff_t i_pos)
506{
507	return hash_32(i_pos, EXFAT_HASH_BITS);
508}
509
510void exfat_hash_inode(struct inode *inode, loff_t i_pos)
511{
512	struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
513	struct hlist_head *head = sbi->inode_hashtable + exfat_hash(i_pos);
514
515	spin_lock(&sbi->inode_hash_lock);
516	EXFAT_I(inode)->i_pos = i_pos;
517	hlist_add_head(&EXFAT_I(inode)->i_hash_fat, head);
518	spin_unlock(&sbi->inode_hash_lock);
519}
520
521void exfat_unhash_inode(struct inode *inode)
522{
523	struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
524
525	spin_lock(&sbi->inode_hash_lock);
526	hlist_del_init(&EXFAT_I(inode)->i_hash_fat);
527	EXFAT_I(inode)->i_pos = 0;
528	spin_unlock(&sbi->inode_hash_lock);
529}
530
531struct inode *exfat_iget(struct super_block *sb, loff_t i_pos)
532{
533	struct exfat_sb_info *sbi = EXFAT_SB(sb);
534	struct exfat_inode_info *info;
535	struct hlist_head *head = sbi->inode_hashtable + exfat_hash(i_pos);
536	struct inode *inode = NULL;
537
538	spin_lock(&sbi->inode_hash_lock);
539	hlist_for_each_entry(info, head, i_hash_fat) {
540		WARN_ON(info->vfs_inode.i_sb != sb);
541
542		if (i_pos != info->i_pos)
543			continue;
544		inode = igrab(&info->vfs_inode);
545		if (inode)
546			break;
547	}
548	spin_unlock(&sbi->inode_hash_lock);
549	return inode;
550}
551
552/* doesn't deal with root inode */
553static int exfat_fill_inode(struct inode *inode, struct exfat_dir_entry *info)
554{
555	struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
556	struct exfat_inode_info *ei = EXFAT_I(inode);
557	loff_t size = info->size;
558
559	memcpy(&ei->dir, &info->dir, sizeof(struct exfat_chain));
560	ei->entry = info->entry;
561	ei->attr = info->attr;
562	ei->start_clu = info->start_clu;
563	ei->flags = info->flags;
564	ei->type = info->type;
 
565
566	ei->version = 0;
567	ei->hint_stat.eidx = 0;
568	ei->hint_stat.clu = info->start_clu;
569	ei->hint_femp.eidx = EXFAT_HINT_NONE;
570	ei->rwoffset = 0;
571	ei->hint_bmap.off = EXFAT_EOF_CLUSTER;
572	ei->i_pos = 0;
573
574	inode->i_uid = sbi->options.fs_uid;
575	inode->i_gid = sbi->options.fs_gid;
576	inode_inc_iversion(inode);
577	inode->i_generation = prandom_u32();
578
579	if (info->attr & ATTR_SUBDIR) { /* directory */
580		inode->i_generation &= ~1;
581		inode->i_mode = exfat_make_mode(sbi, info->attr, 0777);
582		inode->i_op = &exfat_dir_inode_operations;
583		inode->i_fop = &exfat_dir_operations;
584		set_nlink(inode, info->num_subdirs);
585	} else { /* regular file */
586		inode->i_generation |= 1;
587		inode->i_mode = exfat_make_mode(sbi, info->attr, 0777);
588		inode->i_op = &exfat_file_inode_operations;
589		inode->i_fop = &exfat_file_operations;
590		inode->i_mapping->a_ops = &exfat_aops;
591		inode->i_mapping->nrpages = 0;
592	}
593
594	i_size_write(inode, size);
595
596	/* ondisk and aligned size should be aligned with block size */
597	if (size & (inode->i_sb->s_blocksize - 1)) {
598		size |= (inode->i_sb->s_blocksize - 1);
599		size++;
600	}
601
602	ei->i_size_aligned = size;
603	ei->i_size_ondisk = size;
604
605	exfat_save_attr(inode, info->attr);
606
607	inode->i_blocks = ((i_size_read(inode) + (sbi->cluster_size - 1)) &
608		~(sbi->cluster_size - 1)) >> inode->i_blkbits;
609	inode->i_mtime = info->mtime;
610	inode->i_ctime = info->mtime;
611	ei->i_crtime = info->crtime;
612	inode->i_atime = info->atime;
613
614	return 0;
615}
616
617struct inode *exfat_build_inode(struct super_block *sb,
618		struct exfat_dir_entry *info, loff_t i_pos)
619{
620	struct inode *inode;
621	int err;
622
623	inode = exfat_iget(sb, i_pos);
624	if (inode)
625		goto out;
626	inode = new_inode(sb);
627	if (!inode) {
628		inode = ERR_PTR(-ENOMEM);
629		goto out;
630	}
631	inode->i_ino = iunique(sb, EXFAT_ROOT_INO);
632	inode_set_iversion(inode, 1);
633	err = exfat_fill_inode(inode, info);
634	if (err) {
635		iput(inode);
636		inode = ERR_PTR(err);
637		goto out;
638	}
639	exfat_hash_inode(inode, i_pos);
640	insert_inode_hash(inode);
641out:
642	return inode;
643}
644
645void exfat_evict_inode(struct inode *inode)
646{
647	truncate_inode_pages(&inode->i_data, 0);
648
649	if (!inode->i_nlink) {
650		i_size_write(inode, 0);
651		mutex_lock(&EXFAT_SB(inode->i_sb)->s_lock);
652		__exfat_truncate(inode, 0);
653		mutex_unlock(&EXFAT_SB(inode->i_sb)->s_lock);
654	}
655
656	invalidate_inode_buffers(inode);
657	clear_inode(inode);
658	exfat_cache_inval_inode(inode);
659	exfat_unhash_inode(inode);
660}
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
  4 */
  5
  6#include <linux/init.h>
  7#include <linux/buffer_head.h>
  8#include <linux/mpage.h>
  9#include <linux/bio.h>
 10#include <linux/blkdev.h>
 11#include <linux/time.h>
 12#include <linux/writeback.h>
 13#include <linux/uio.h>
 14#include <linux/random.h>
 15#include <linux/iversion.h>
 16
 17#include "exfat_raw.h"
 18#include "exfat_fs.h"
 19
 20int __exfat_write_inode(struct inode *inode, int sync)
 21{
 22	unsigned long long on_disk_size;
 23	struct exfat_dentry *ep, *ep2;
 24	struct exfat_entry_set_cache es;
 25	struct super_block *sb = inode->i_sb;
 26	struct exfat_sb_info *sbi = EXFAT_SB(sb);
 27	struct exfat_inode_info *ei = EXFAT_I(inode);
 28	bool is_dir = (ei->type == TYPE_DIR) ? true : false;
 29	struct timespec64 ts;
 30
 31	if (inode->i_ino == EXFAT_ROOT_INO)
 32		return 0;
 33
 34	/*
 35	 * If the inode is already unlinked, there is no need for updating it.
 36	 */
 37	if (ei->dir.dir == DIR_DELETED)
 38		return 0;
 39
 40	if (is_dir && ei->dir.dir == sbi->root_dir && ei->entry == -1)
 41		return 0;
 42
 43	exfat_set_volume_dirty(sb);
 44
 45	/* get the directory entry of given file or directory */
 46	if (exfat_get_dentry_set_by_ei(&es, sb, ei))
 
 47		return -EIO;
 48	ep = exfat_get_dentry_cached(&es, ES_IDX_FILE);
 49	ep2 = exfat_get_dentry_cached(&es, ES_IDX_STREAM);
 50
 51	ep->dentry.file.attr = cpu_to_le16(exfat_make_attr(inode));
 52
 53	/* set FILE_INFO structure using the acquired struct exfat_dentry */
 54	exfat_set_entry_time(sbi, &ei->i_crtime,
 55			&ep->dentry.file.create_tz,
 56			&ep->dentry.file.create_time,
 57			&ep->dentry.file.create_date,
 58			&ep->dentry.file.create_time_cs);
 59	ts = inode_get_mtime(inode);
 60	exfat_set_entry_time(sbi, &ts,
 61			     &ep->dentry.file.modify_tz,
 62			     &ep->dentry.file.modify_time,
 63			     &ep->dentry.file.modify_date,
 64			     &ep->dentry.file.modify_time_cs);
 65	ts = inode_get_atime(inode);
 66	exfat_set_entry_time(sbi, &ts,
 67			     &ep->dentry.file.access_tz,
 68			     &ep->dentry.file.access_time,
 69			     &ep->dentry.file.access_date,
 70			     NULL);
 71
 72	/* File size should be zero if there is no cluster allocated */
 73	on_disk_size = i_size_read(inode);
 74
 75	if (ei->start_clu == EXFAT_EOF_CLUSTER)
 76		on_disk_size = 0;
 77
 78	ep2->dentry.stream.size = cpu_to_le64(on_disk_size);
 79	/*
 80	 * mmap write does not use exfat_write_end(), valid_size may be
 81	 * extended to the sector-aligned length in exfat_get_block().
 82	 * So we need to fixup valid_size to the writren length.
 83	 */
 84	if (on_disk_size < ei->valid_size)
 85		ep2->dentry.stream.valid_size = ep2->dentry.stream.size;
 86	else
 87		ep2->dentry.stream.valid_size = cpu_to_le64(ei->valid_size);
 88
 89	if (on_disk_size) {
 90		ep2->dentry.stream.flags = ei->flags;
 91		ep2->dentry.stream.start_clu = cpu_to_le32(ei->start_clu);
 92	} else {
 93		ep2->dentry.stream.flags = ALLOC_FAT_CHAIN;
 94		ep2->dentry.stream.start_clu = EXFAT_FREE_CLUSTER;
 95	}
 96
 97	exfat_update_dir_chksum(&es);
 98	return exfat_put_dentry_set(&es, sync);
 99}
100
101int exfat_write_inode(struct inode *inode, struct writeback_control *wbc)
102{
103	int ret;
104
105	if (unlikely(exfat_forced_shutdown(inode->i_sb)))
106		return -EIO;
107
108	mutex_lock(&EXFAT_SB(inode->i_sb)->s_lock);
109	ret = __exfat_write_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
110	mutex_unlock(&EXFAT_SB(inode->i_sb)->s_lock);
111
112	return ret;
113}
114
115void exfat_sync_inode(struct inode *inode)
116{
117	lockdep_assert_held(&EXFAT_SB(inode->i_sb)->s_lock);
118	__exfat_write_inode(inode, 1);
119}
120
121/*
122 * Input: inode, (logical) clu_offset, target allocation area
123 * Output: errcode, cluster number
124 * *clu = (~0), if it's unable to allocate a new cluster
125 */
126static int exfat_map_cluster(struct inode *inode, unsigned int clu_offset,
127		unsigned int *clu, int create)
128{
129	int ret;
130	unsigned int last_clu;
131	struct exfat_chain new_clu;
132	struct super_block *sb = inode->i_sb;
133	struct exfat_sb_info *sbi = EXFAT_SB(sb);
134	struct exfat_inode_info *ei = EXFAT_I(inode);
135	unsigned int local_clu_offset = clu_offset;
136	unsigned int num_to_be_allocated = 0, num_clusters;
137
138	num_clusters = EXFAT_B_TO_CLU(exfat_ondisk_size(inode), sbi);
 
 
 
 
 
139
140	if (clu_offset >= num_clusters)
141		num_to_be_allocated = clu_offset - num_clusters + 1;
142
143	if (!create && (num_to_be_allocated > 0)) {
144		*clu = EXFAT_EOF_CLUSTER;
145		return 0;
146	}
147
148	*clu = last_clu = ei->start_clu;
149
150	if (ei->flags == ALLOC_NO_FAT_CHAIN) {
151		if (clu_offset > 0 && *clu != EXFAT_EOF_CLUSTER) {
152			last_clu += clu_offset - 1;
153
154			if (clu_offset == num_clusters)
155				*clu = EXFAT_EOF_CLUSTER;
156			else
157				*clu += clu_offset;
158		}
159	} else if (ei->type == TYPE_FILE) {
160		unsigned int fclus = 0;
161		int err = exfat_get_cluster(inode, clu_offset,
162				&fclus, clu, &last_clu, 1);
163		if (err)
164			return -EIO;
165
166		clu_offset -= fclus;
167	} else {
168		/* hint information */
169		if (clu_offset > 0 && ei->hint_bmap.off != EXFAT_EOF_CLUSTER &&
170		    ei->hint_bmap.off > 0 && clu_offset >= ei->hint_bmap.off) {
171			clu_offset -= ei->hint_bmap.off;
172			/* hint_bmap.clu should be valid */
173			WARN_ON(ei->hint_bmap.clu < 2);
174			*clu = ei->hint_bmap.clu;
175		}
176
177		while (clu_offset > 0 && *clu != EXFAT_EOF_CLUSTER) {
178			last_clu = *clu;
179			if (exfat_get_next_cluster(sb, clu))
180				return -EIO;
181			clu_offset--;
182		}
183	}
184
185	if (*clu == EXFAT_EOF_CLUSTER) {
186		exfat_set_volume_dirty(sb);
187
188		new_clu.dir = (last_clu == EXFAT_EOF_CLUSTER) ?
189				EXFAT_EOF_CLUSTER : last_clu + 1;
190		new_clu.size = 0;
191		new_clu.flags = ei->flags;
192
193		/* allocate a cluster */
194		if (num_to_be_allocated < 1) {
195			/* Broken FAT (i_sze > allocated FAT) */
196			exfat_fs_error(sb, "broken FAT chain.");
197			return -EIO;
198		}
199
200		ret = exfat_alloc_cluster(inode, num_to_be_allocated, &new_clu,
201				inode_needs_sync(inode));
202		if (ret)
203			return ret;
204
205		if (new_clu.dir == EXFAT_EOF_CLUSTER ||
206		    new_clu.dir == EXFAT_FREE_CLUSTER) {
207			exfat_fs_error(sb,
208				"bogus cluster new allocated (last_clu : %u, new_clu : %u)",
209				last_clu, new_clu.dir);
210			return -EIO;
211		}
212
213		/* append to the FAT chain */
214		if (last_clu == EXFAT_EOF_CLUSTER) {
215			if (new_clu.flags == ALLOC_FAT_CHAIN)
216				ei->flags = ALLOC_FAT_CHAIN;
217			ei->start_clu = new_clu.dir;
 
218		} else {
219			if (new_clu.flags != ei->flags) {
220				/* no-fat-chain bit is disabled,
221				 * so fat-chain should be synced with
222				 * alloc-bitmap
223				 */
224				exfat_chain_cont_cluster(sb, ei->start_clu,
225					num_clusters);
226				ei->flags = ALLOC_FAT_CHAIN;
 
227			}
228			if (new_clu.flags == ALLOC_FAT_CHAIN)
229				if (exfat_ent_set(sb, last_clu, new_clu.dir))
230					return -EIO;
231		}
232
233		num_clusters += num_to_be_allocated;
234		*clu = new_clu.dir;
235
236		inode->i_blocks += EXFAT_CLU_TO_B(num_to_be_allocated, sbi) >> 9;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
237
238		/*
239		 * Move *clu pointer along FAT chains (hole care) because the
240		 * caller of this function expect *clu to be the last cluster.
241		 * This only works when num_to_be_allocated >= 2,
242		 * *clu = (the first cluster of the allocated chain) =>
243		 * (the last cluster of ...)
244		 */
245		if (ei->flags == ALLOC_NO_FAT_CHAIN) {
246			*clu += num_to_be_allocated - 1;
247		} else {
248			while (num_to_be_allocated > 1) {
249				if (exfat_get_next_cluster(sb, clu))
250					return -EIO;
251				num_to_be_allocated--;
252			}
253		}
254
255	}
256
257	/* hint information */
258	ei->hint_bmap.off = local_clu_offset;
259	ei->hint_bmap.clu = *clu;
260
261	return 0;
262}
263
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
264static int exfat_get_block(struct inode *inode, sector_t iblock,
265		struct buffer_head *bh_result, int create)
266{
267	struct exfat_inode_info *ei = EXFAT_I(inode);
268	struct super_block *sb = inode->i_sb;
269	struct exfat_sb_info *sbi = EXFAT_SB(sb);
270	unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits;
271	int err = 0;
272	unsigned long mapped_blocks = 0;
273	unsigned int cluster, sec_offset;
274	sector_t last_block;
275	sector_t phys = 0;
276	sector_t valid_blks;
277
278	mutex_lock(&sbi->s_lock);
279	last_block = EXFAT_B_TO_BLK_ROUND_UP(i_size_read(inode), sb);
280	if (iblock >= last_block && !create)
281		goto done;
282
283	/* Is this block already allocated? */
284	err = exfat_map_cluster(inode, iblock >> sbi->sect_per_clus_bits,
285			&cluster, create);
286	if (err) {
287		if (err != -ENOSPC)
288			exfat_fs_error_ratelimit(sb,
289				"failed to bmap (inode : %p iblock : %llu, err : %d)",
290				inode, (unsigned long long)iblock, err);
291		goto unlock_ret;
292	}
293
294	if (cluster == EXFAT_EOF_CLUSTER)
295		goto done;
296
297	/* sector offset in cluster */
298	sec_offset = iblock & (sbi->sect_per_clus - 1);
299
300	phys = exfat_cluster_to_sector(sbi, cluster) + sec_offset;
301	mapped_blocks = sbi->sect_per_clus - sec_offset;
302	max_blocks = min(mapped_blocks, max_blocks);
303
304	map_bh(bh_result, sb, phys);
305	if (buffer_delay(bh_result))
306		clear_buffer_delay(bh_result);
 
 
 
 
 
 
307
308	if (create) {
309		valid_blks = EXFAT_B_TO_BLK_ROUND_UP(ei->valid_size, sb);
310
311		if (iblock + max_blocks < valid_blks) {
312			/* The range has been written, map it */
313			goto done;
314		} else if (iblock < valid_blks) {
315			/*
316			 * The range has been partially written,
317			 * map the written part.
318			 */
319			max_blocks = valid_blks - iblock;
320			goto done;
321		}
 
322
323		/* The area has not been written, map and mark as new. */
324		set_buffer_new(bh_result);
325
326		ei->valid_size = EXFAT_BLK_TO_B(iblock + max_blocks, sb);
327		mark_inode_dirty(inode);
328	} else {
329		valid_blks = EXFAT_B_TO_BLK(ei->valid_size, sb);
330
331		if (iblock + max_blocks < valid_blks) {
332			/* The range has been written, map it */
333			goto done;
334		} else if (iblock < valid_blks) {
335			/*
336			 * The area has been partially written,
337			 * map the written part.
338			 */
339			max_blocks = valid_blks - iblock;
340			goto done;
341		} else if (iblock == valid_blks &&
342			   (ei->valid_size & (sb->s_blocksize - 1))) {
343			/*
344			 * The block has been partially written,
345			 * zero the unwritten part and map the block.
346			 */
347			loff_t size, off, pos;
348
349			max_blocks = 1;
350
351			/*
352			 * For direct read, the unwritten part will be zeroed in
353			 * exfat_direct_IO()
354			 */
355			if (!bh_result->b_folio)
356				goto done;
357
358			pos = EXFAT_BLK_TO_B(iblock, sb);
359			size = ei->valid_size - pos;
360			off = pos & (PAGE_SIZE - 1);
361
362			folio_set_bh(bh_result, bh_result->b_folio, off);
363			err = bh_read(bh_result, 0);
364			if (err < 0)
365				goto unlock_ret;
366
367			folio_zero_segment(bh_result->b_folio, off + size,
368					off + sb->s_blocksize);
369		} else {
370			/*
371			 * The range has not been written, clear the mapped flag
372			 * to only zero the cache and do not read from disk.
373			 */
374			clear_buffer_mapped(bh_result);
375		}
376	}
377done:
378	bh_result->b_size = EXFAT_BLK_TO_B(max_blocks, sb);
379unlock_ret:
380	mutex_unlock(&sbi->s_lock);
381	return err;
382}
383
384static int exfat_read_folio(struct file *file, struct folio *folio)
385{
386	return mpage_read_folio(folio, exfat_get_block);
387}
388
389static void exfat_readahead(struct readahead_control *rac)
390{
391	struct address_space *mapping = rac->mapping;
392	struct inode *inode = mapping->host;
393	struct exfat_inode_info *ei = EXFAT_I(inode);
394	loff_t pos = readahead_pos(rac);
395
396	/* Range cross valid_size, read it page by page. */
397	if (ei->valid_size < i_size_read(inode) &&
398	    pos <= ei->valid_size &&
399	    ei->valid_size < pos + readahead_length(rac))
400		return;
401
402	mpage_readahead(rac, exfat_get_block);
403}
404
405static int exfat_writepages(struct address_space *mapping,
406		struct writeback_control *wbc)
407{
408	if (unlikely(exfat_forced_shutdown(mapping->host->i_sb)))
409		return -EIO;
410
411	return mpage_writepages(mapping, wbc, exfat_get_block);
412}
413
414static void exfat_write_failed(struct address_space *mapping, loff_t to)
415{
416	struct inode *inode = mapping->host;
417
418	if (to > i_size_read(inode)) {
419		truncate_pagecache(inode, i_size_read(inode));
420		inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
421		exfat_truncate(inode);
422	}
423}
424
425static int exfat_write_begin(struct file *file, struct address_space *mapping,
426		loff_t pos, unsigned int len,
427		struct folio **foliop, void **fsdata)
428{
429	int ret;
430
431	if (unlikely(exfat_forced_shutdown(mapping->host->i_sb)))
432		return -EIO;
433
434	ret = block_write_begin(mapping, pos, len, foliop, exfat_get_block);
435
436	if (ret < 0)
437		exfat_write_failed(mapping, pos+len);
438
439	return ret;
440}
441
442static int exfat_write_end(struct file *file, struct address_space *mapping,
443		loff_t pos, unsigned int len, unsigned int copied,
444		struct folio *folio, void *fsdata)
445{
446	struct inode *inode = mapping->host;
447	struct exfat_inode_info *ei = EXFAT_I(inode);
448	int err;
449
450	err = generic_write_end(file, mapping, pos, len, copied, folio, fsdata);
 
 
 
 
 
 
 
 
451	if (err < len)
452		exfat_write_failed(mapping, pos+len);
453
454	if (!(err < 0) && pos + err > ei->valid_size) {
455		ei->valid_size = pos + err;
456		mark_inode_dirty(inode);
457	}
458
459	if (!(err < 0) && !(ei->attr & EXFAT_ATTR_ARCHIVE)) {
460		inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
461		ei->attr |= EXFAT_ATTR_ARCHIVE;
462		mark_inode_dirty(inode);
463	}
464
465	return err;
466}
467
468static ssize_t exfat_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
469{
470	struct address_space *mapping = iocb->ki_filp->f_mapping;
471	struct inode *inode = mapping->host;
472	struct exfat_inode_info *ei = EXFAT_I(inode);
473	loff_t pos = iocb->ki_pos;
474	loff_t size = pos + iov_iter_count(iter);
475	int rw = iov_iter_rw(iter);
476	ssize_t ret;
477
 
 
 
 
 
 
 
 
 
 
 
 
 
 
478	/*
479	 * Need to use the DIO_LOCKING for avoiding the race
480	 * condition of exfat_get_block() and ->truncate().
481	 */
482	ret = blockdev_direct_IO(iocb, inode, iter, exfat_get_block);
483	if (ret < 0) {
484		if (rw == WRITE && ret != -EIOCBQUEUED)
485			exfat_write_failed(mapping, size);
486
487		return ret;
488	} else
489		size = pos + ret;
490
491	if (rw == WRITE) {
492		/*
493		 * If the block had been partially written before this write,
494		 * ->valid_size will not be updated in exfat_get_block(),
495		 * update it here.
496		 */
497		if (ei->valid_size < size) {
498			ei->valid_size = size;
499			mark_inode_dirty(inode);
500		}
501	} else if (pos < ei->valid_size && ei->valid_size < size) {
502		/* zero the unwritten part in the partially written block */
503		iov_iter_revert(iter, size - ei->valid_size);
504		iov_iter_zero(size - ei->valid_size, iter);
505	}
506
507	return ret;
508}
509
510static sector_t exfat_aop_bmap(struct address_space *mapping, sector_t block)
511{
512	sector_t blocknr;
513
514	/* exfat_get_cluster() assumes the requested blocknr isn't truncated. */
515	down_read(&EXFAT_I(mapping->host)->truncate_lock);
516	blocknr = generic_block_bmap(mapping, block, exfat_get_block);
517	up_read(&EXFAT_I(mapping->host)->truncate_lock);
518	return blocknr;
519}
520
521/*
522 * exfat_block_truncate_page() zeroes out a mapping from file offset `from'
523 * up to the end of the block which corresponds to `from'.
524 * This is required during truncate to physically zeroout the tail end
525 * of that block so it doesn't yield old data if the file is later grown.
526 * Also, avoid causing failure from fsx for cases of "data past EOF"
527 */
528int exfat_block_truncate_page(struct inode *inode, loff_t from)
529{
530	return block_truncate_page(inode->i_mapping, from, exfat_get_block);
531}
532
533static const struct address_space_operations exfat_aops = {
534	.dirty_folio	= block_dirty_folio,
535	.invalidate_folio = block_invalidate_folio,
536	.read_folio	= exfat_read_folio,
537	.readahead	= exfat_readahead,
 
538	.writepages	= exfat_writepages,
539	.write_begin	= exfat_write_begin,
540	.write_end	= exfat_write_end,
541	.direct_IO	= exfat_direct_IO,
542	.bmap		= exfat_aop_bmap,
543	.migrate_folio	= buffer_migrate_folio,
544};
545
546static inline unsigned long exfat_hash(loff_t i_pos)
547{
548	return hash_32(i_pos, EXFAT_HASH_BITS);
549}
550
551void exfat_hash_inode(struct inode *inode, loff_t i_pos)
552{
553	struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
554	struct hlist_head *head = sbi->inode_hashtable + exfat_hash(i_pos);
555
556	spin_lock(&sbi->inode_hash_lock);
557	EXFAT_I(inode)->i_pos = i_pos;
558	hlist_add_head(&EXFAT_I(inode)->i_hash_fat, head);
559	spin_unlock(&sbi->inode_hash_lock);
560}
561
562void exfat_unhash_inode(struct inode *inode)
563{
564	struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
565
566	spin_lock(&sbi->inode_hash_lock);
567	hlist_del_init(&EXFAT_I(inode)->i_hash_fat);
568	EXFAT_I(inode)->i_pos = 0;
569	spin_unlock(&sbi->inode_hash_lock);
570}
571
572struct inode *exfat_iget(struct super_block *sb, loff_t i_pos)
573{
574	struct exfat_sb_info *sbi = EXFAT_SB(sb);
575	struct exfat_inode_info *info;
576	struct hlist_head *head = sbi->inode_hashtable + exfat_hash(i_pos);
577	struct inode *inode = NULL;
578
579	spin_lock(&sbi->inode_hash_lock);
580	hlist_for_each_entry(info, head, i_hash_fat) {
581		WARN_ON(info->vfs_inode.i_sb != sb);
582
583		if (i_pos != info->i_pos)
584			continue;
585		inode = igrab(&info->vfs_inode);
586		if (inode)
587			break;
588	}
589	spin_unlock(&sbi->inode_hash_lock);
590	return inode;
591}
592
593/* doesn't deal with root inode */
594static int exfat_fill_inode(struct inode *inode, struct exfat_dir_entry *info)
595{
596	struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
597	struct exfat_inode_info *ei = EXFAT_I(inode);
598	loff_t size = info->size;
599
600	ei->dir = info->dir;
601	ei->entry = info->entry;
602	ei->attr = info->attr;
603	ei->start_clu = info->start_clu;
604	ei->flags = info->flags;
605	ei->type = info->type;
606	ei->valid_size = info->valid_size;
607
608	ei->version = 0;
609	ei->hint_stat.eidx = 0;
610	ei->hint_stat.clu = info->start_clu;
611	ei->hint_femp.eidx = EXFAT_HINT_NONE;
 
612	ei->hint_bmap.off = EXFAT_EOF_CLUSTER;
613	ei->i_pos = 0;
614
615	inode->i_uid = sbi->options.fs_uid;
616	inode->i_gid = sbi->options.fs_gid;
617	inode_inc_iversion(inode);
618	inode->i_generation = get_random_u32();
619
620	if (info->attr & EXFAT_ATTR_SUBDIR) { /* directory */
621		inode->i_generation &= ~1;
622		inode->i_mode = exfat_make_mode(sbi, info->attr, 0777);
623		inode->i_op = &exfat_dir_inode_operations;
624		inode->i_fop = &exfat_dir_operations;
625		set_nlink(inode, info->num_subdirs);
626	} else { /* regular file */
627		inode->i_generation |= 1;
628		inode->i_mode = exfat_make_mode(sbi, info->attr, 0777);
629		inode->i_op = &exfat_file_inode_operations;
630		inode->i_fop = &exfat_file_operations;
631		inode->i_mapping->a_ops = &exfat_aops;
632		inode->i_mapping->nrpages = 0;
633	}
634
635	i_size_write(inode, size);
636
 
 
 
 
 
 
 
 
 
637	exfat_save_attr(inode, info->attr);
638
639	inode->i_blocks = round_up(i_size_read(inode), sbi->cluster_size) >> 9;
640	inode_set_mtime_to_ts(inode, info->mtime);
641	inode_set_ctime_to_ts(inode, info->mtime);
 
642	ei->i_crtime = info->crtime;
643	inode_set_atime_to_ts(inode, info->atime);
644
645	return 0;
646}
647
648struct inode *exfat_build_inode(struct super_block *sb,
649		struct exfat_dir_entry *info, loff_t i_pos)
650{
651	struct inode *inode;
652	int err;
653
654	inode = exfat_iget(sb, i_pos);
655	if (inode)
656		goto out;
657	inode = new_inode(sb);
658	if (!inode) {
659		inode = ERR_PTR(-ENOMEM);
660		goto out;
661	}
662	inode->i_ino = iunique(sb, EXFAT_ROOT_INO);
663	inode_set_iversion(inode, 1);
664	err = exfat_fill_inode(inode, info);
665	if (err) {
666		iput(inode);
667		inode = ERR_PTR(err);
668		goto out;
669	}
670	exfat_hash_inode(inode, i_pos);
671	insert_inode_hash(inode);
672out:
673	return inode;
674}
675
676void exfat_evict_inode(struct inode *inode)
677{
678	truncate_inode_pages(&inode->i_data, 0);
679
680	if (!inode->i_nlink) {
681		i_size_write(inode, 0);
682		mutex_lock(&EXFAT_SB(inode->i_sb)->s_lock);
683		__exfat_truncate(inode);
684		mutex_unlock(&EXFAT_SB(inode->i_sb)->s_lock);
685	}
686
687	invalidate_inode_buffers(inode);
688	clear_inode(inode);
689	exfat_cache_inval_inode(inode);
690	exfat_unhash_inode(inode);
691}