| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  *  linux/fs/fat/misc.c | 
 | 3 |  * | 
 | 4 |  *  Written 1992,1993 by Werner Almesberger | 
 | 5 |  *  22/11/2000 - Fixed fat_date_unix2dos for dates earlier than 01/01/1980 | 
 | 6 |  *		 and date_dos2unix for date==0 by Igor Zhbanov(bsg@uniyar.ac.ru) | 
 | 7 |  */ | 
 | 8 |  | 
 | 9 | #include <linux/module.h> | 
 | 10 | #include <linux/fs.h> | 
 | 11 | #include <linux/msdos_fs.h> | 
 | 12 | #include <linux/buffer_head.h> | 
 | 13 |  | 
 | 14 | /* | 
 | 15 |  * fat_fs_panic reports a severe file system problem and sets the file system | 
 | 16 |  * read-only. The file system can be made writable again by remounting it. | 
 | 17 |  */ | 
 | 18 | void fat_fs_panic(struct super_block *s, const char *fmt, ...) | 
 | 19 | { | 
 | 20 | 	va_list args; | 
 | 21 |  | 
 | 22 | 	printk(KERN_ERR "FAT: Filesystem panic (dev %s)\n", s->s_id); | 
 | 23 |  | 
 | 24 | 	printk(KERN_ERR "    "); | 
 | 25 | 	va_start(args, fmt); | 
 | 26 | 	vprintk(fmt, args); | 
 | 27 | 	va_end(args); | 
 | 28 | 	printk("\n"); | 
 | 29 |  | 
 | 30 | 	if (!(s->s_flags & MS_RDONLY)) { | 
 | 31 | 		s->s_flags |= MS_RDONLY; | 
 | 32 | 		printk(KERN_ERR "    File system has been set read-only\n"); | 
 | 33 | 	} | 
 | 34 | } | 
 | 35 |  | 
| OGAWA Hirofumi | 7c709d0 | 2006-01-08 01:02:10 -0800 | [diff] [blame] | 36 | EXPORT_SYMBOL_GPL(fat_fs_panic); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 |  | 
 | 38 | /* Flushes the number of free clusters on FAT32 */ | 
 | 39 | /* XXX: Need to write one per FSINFO block.  Currently only writes 1 */ | 
 | 40 | void fat_clusters_flush(struct super_block *sb) | 
 | 41 | { | 
 | 42 | 	struct msdos_sb_info *sbi = MSDOS_SB(sb); | 
 | 43 | 	struct buffer_head *bh; | 
 | 44 | 	struct fat_boot_fsinfo *fsinfo; | 
 | 45 |  | 
 | 46 | 	if (sbi->fat_bits != 32) | 
 | 47 | 		return; | 
 | 48 |  | 
 | 49 | 	bh = sb_bread(sb, sbi->fsinfo_sector); | 
 | 50 | 	if (bh == NULL) { | 
 | 51 | 		printk(KERN_ERR "FAT: bread failed in fat_clusters_flush\n"); | 
 | 52 | 		return; | 
 | 53 | 	} | 
 | 54 |  | 
 | 55 | 	fsinfo = (struct fat_boot_fsinfo *)bh->b_data; | 
 | 56 | 	/* Sanity check */ | 
 | 57 | 	if (!IS_FSINFO(fsinfo)) { | 
 | 58 | 		printk(KERN_ERR "FAT: Did not find valid FSINFO signature.\n" | 
 | 59 | 		       "     Found signature1 0x%08x signature2 0x%08x" | 
 | 60 | 		       " (sector = %lu)\n", | 
 | 61 | 		       le32_to_cpu(fsinfo->signature1), | 
 | 62 | 		       le32_to_cpu(fsinfo->signature2), | 
 | 63 | 		       sbi->fsinfo_sector); | 
 | 64 | 	} else { | 
 | 65 | 		if (sbi->free_clusters != -1) | 
 | 66 | 			fsinfo->free_clusters = cpu_to_le32(sbi->free_clusters); | 
 | 67 | 		if (sbi->prev_free != -1) | 
 | 68 | 			fsinfo->next_cluster = cpu_to_le32(sbi->prev_free); | 
 | 69 | 		mark_buffer_dirty(bh); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | 	} | 
 | 71 | 	brelse(bh); | 
 | 72 | } | 
 | 73 |  | 
 | 74 | /* | 
 | 75 |  * fat_chain_add() adds a new cluster to the chain of clusters represented | 
 | 76 |  * by inode. | 
 | 77 |  */ | 
 | 78 | int fat_chain_add(struct inode *inode, int new_dclus, int nr_cluster) | 
 | 79 | { | 
 | 80 | 	struct super_block *sb = inode->i_sb; | 
 | 81 | 	struct msdos_sb_info *sbi = MSDOS_SB(sb); | 
 | 82 | 	int ret, new_fclus, last; | 
 | 83 |  | 
 | 84 | 	/* | 
 | 85 | 	 * We must locate the last cluster of the file to add this new | 
 | 86 | 	 * one (new_dclus) to the end of the link list (the FAT). | 
 | 87 | 	 */ | 
 | 88 | 	last = new_fclus = 0; | 
 | 89 | 	if (MSDOS_I(inode)->i_start) { | 
 | 90 | 		int fclus, dclus; | 
 | 91 |  | 
 | 92 | 		ret = fat_get_cluster(inode, FAT_ENT_EOF, &fclus, &dclus); | 
 | 93 | 		if (ret < 0) | 
 | 94 | 			return ret; | 
 | 95 | 		new_fclus = fclus + 1; | 
 | 96 | 		last = dclus; | 
 | 97 | 	} | 
 | 98 |  | 
 | 99 | 	/* add new one to the last of the cluster chain */ | 
 | 100 | 	if (last) { | 
 | 101 | 		struct fat_entry fatent; | 
 | 102 |  | 
 | 103 | 		fatent_init(&fatent); | 
 | 104 | 		ret = fat_ent_read(inode, &fatent, last); | 
 | 105 | 		if (ret >= 0) { | 
 | 106 | 			int wait = inode_needs_sync(inode); | 
 | 107 | 			ret = fat_ent_write(inode, &fatent, new_dclus, wait); | 
 | 108 | 			fatent_brelse(&fatent); | 
 | 109 | 		} | 
 | 110 | 		if (ret < 0) | 
 | 111 | 			return ret; | 
 | 112 | //		fat_cache_add(inode, new_fclus, new_dclus); | 
 | 113 | 	} else { | 
 | 114 | 		MSDOS_I(inode)->i_start = new_dclus; | 
 | 115 | 		MSDOS_I(inode)->i_logstart = new_dclus; | 
 | 116 | 		/* | 
 | 117 | 		 * Since generic_osync_inode() synchronize later if | 
 | 118 | 		 * this is not directory, we don't here. | 
 | 119 | 		 */ | 
 | 120 | 		if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode)) { | 
 | 121 | 			ret = fat_sync_inode(inode); | 
 | 122 | 			if (ret) | 
 | 123 | 				return ret; | 
 | 124 | 		} else | 
 | 125 | 			mark_inode_dirty(inode); | 
 | 126 | 	} | 
 | 127 | 	if (new_fclus != (inode->i_blocks >> (sbi->cluster_bits - 9))) { | 
 | 128 | 		fat_fs_panic(sb, "clusters badly computed (%d != %lu)", | 
 | 129 | 			new_fclus, inode->i_blocks >> (sbi->cluster_bits - 9)); | 
 | 130 | 		fat_cache_inval_inode(inode); | 
 | 131 | 	} | 
 | 132 | 	inode->i_blocks += nr_cluster << (sbi->cluster_bits - 9); | 
 | 133 |  | 
 | 134 | 	return 0; | 
 | 135 | } | 
 | 136 |  | 
 | 137 | extern struct timezone sys_tz; | 
 | 138 |  | 
 | 139 | /* Linear day numbers of the respective 1sts in non-leap years. */ | 
 | 140 | static int day_n[] = { | 
 | 141 |    /* Jan  Feb  Mar  Apr   May  Jun  Jul  Aug  Sep  Oct  Nov  Dec */ | 
 | 142 | 	0,  31,  59,  90,  120, 151, 181, 212, 243, 273, 304, 334, 0, 0, 0, 0 | 
 | 143 | }; | 
 | 144 |  | 
 | 145 | /* Convert a MS-DOS time/date pair to a UNIX date (seconds since 1 1 70). */ | 
 | 146 | int date_dos2unix(unsigned short time, unsigned short date) | 
 | 147 | { | 
 | 148 | 	int month, year, secs; | 
 | 149 |  | 
 | 150 | 	/* | 
 | 151 | 	 * first subtract and mask after that... Otherwise, if | 
 | 152 | 	 * date == 0, bad things happen | 
 | 153 | 	 */ | 
 | 154 | 	month = ((date >> 5) - 1) & 15; | 
 | 155 | 	year = date >> 9; | 
 | 156 | 	secs = (time & 31)*2+60*((time >> 5) & 63)+(time >> 11)*3600+86400* | 
 | 157 | 	    ((date & 31)-1+day_n[month]+(year/4)+year*365-((year & 3) == 0 && | 
 | 158 | 	    month < 2 ? 1 : 0)+3653); | 
 | 159 | 			/* days since 1.1.70 plus 80's leap day */ | 
 | 160 | 	secs += sys_tz.tz_minuteswest*60; | 
 | 161 | 	return secs; | 
 | 162 | } | 
 | 163 |  | 
 | 164 | /* Convert linear UNIX date to a MS-DOS time/date pair. */ | 
 | 165 | void fat_date_unix2dos(int unix_date, __le16 *time, __le16 *date) | 
 | 166 | { | 
 | 167 | 	int day, year, nl_day, month; | 
 | 168 |  | 
 | 169 | 	unix_date -= sys_tz.tz_minuteswest*60; | 
 | 170 |  | 
 | 171 | 	/* Jan 1 GMT 00:00:00 1980. But what about another time zone? */ | 
 | 172 | 	if (unix_date < 315532800) | 
 | 173 | 		unix_date = 315532800; | 
 | 174 |  | 
 | 175 | 	*time = cpu_to_le16((unix_date % 60)/2+(((unix_date/60) % 60) << 5)+ | 
 | 176 | 	    (((unix_date/3600) % 24) << 11)); | 
 | 177 | 	day = unix_date/86400-3652; | 
 | 178 | 	year = day/365; | 
 | 179 | 	if ((year+3)/4+365*year > day) | 
 | 180 | 		year--; | 
 | 181 | 	day -= (year+3)/4+365*year; | 
 | 182 | 	if (day == 59 && !(year & 3)) { | 
 | 183 | 		nl_day = day; | 
 | 184 | 		month = 2; | 
 | 185 | 	} else { | 
 | 186 | 		nl_day = (year & 3) || day <= 59 ? day : day-1; | 
 | 187 | 		for (month = 0; month < 12; month++) { | 
 | 188 | 			if (day_n[month] > nl_day) | 
 | 189 | 				break; | 
 | 190 | 		} | 
 | 191 | 	} | 
 | 192 | 	*date = cpu_to_le16(nl_day-day_n[month-1]+1+(month << 5)+(year << 9)); | 
 | 193 | } | 
 | 194 |  | 
| OGAWA Hirofumi | 7c709d0 | 2006-01-08 01:02:10 -0800 | [diff] [blame] | 195 | EXPORT_SYMBOL_GPL(fat_date_unix2dos); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 |  | 
 | 197 | int fat_sync_bhs(struct buffer_head **bhs, int nr_bhs) | 
 | 198 | { | 
| OGAWA Hirofumi | 5b00226 | 2006-02-03 03:04:42 -0800 | [diff] [blame] | 199 | 	int i, err = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 |  | 
| OGAWA Hirofumi | 5b00226 | 2006-02-03 03:04:42 -0800 | [diff] [blame] | 201 | 	ll_rw_block(SWRITE, nr_bhs, bhs); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | 	for (i = 0; i < nr_bhs; i++) { | 
 | 203 | 		wait_on_buffer(bhs[i]); | 
 | 204 | 		if (buffer_eopnotsupp(bhs[i])) { | 
 | 205 | 			clear_buffer_eopnotsupp(bhs[i]); | 
 | 206 | 			err = -EOPNOTSUPP; | 
 | 207 | 		} else if (!err && !buffer_uptodate(bhs[i])) | 
 | 208 | 			err = -EIO; | 
 | 209 | 	} | 
 | 210 | 	return err; | 
 | 211 | } | 
 | 212 |  |