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
36 void lock_fat(struct super_block *sb)
37 {
38 down(&(MSDOS_SB(sb)->fat_lock));
39 }
40
41 void unlock_fat(struct super_block *sb)
42 {
43 up(&(MSDOS_SB(sb)->fat_lock));
44 }
45
46 /* Flushes the number of free clusters on FAT32 */
47 /* XXX: Need to write one per FSINFO block. Currently only writes 1 */
48 void fat_clusters_flush(struct super_block *sb)
49 {
50 struct msdos_sb_info *sbi = MSDOS_SB(sb);
51 struct buffer_head *bh;
52 struct fat_boot_fsinfo *fsinfo;
53
54 if (sbi->fat_bits != 32)
55 return;
56
57 bh = sb_bread(sb, sbi->fsinfo_sector);
58 if (bh == NULL) {
59 printk(KERN_ERR "FAT bread failed in fat_clusters_flush\n");
60 return;
61 }
62
63 fsinfo = (struct fat_boot_fsinfo *)bh->b_data;
64 /* Sanity check */
65 if (!IS_FSINFO(fsinfo)) {
66 printk(KERN_ERR "FAT: Did not find valid FSINFO signature.\n"
67 " Found signature1 0x%08x signature2 0x%08x"
68 " (sector = %lu)\n",
69 le32_to_cpu(fsinfo->signature1),
70 le32_to_cpu(fsinfo->signature2),
71 sbi->fsinfo_sector);
72 } else {
73 if (sbi->free_clusters != -1)
74 fsinfo->free_clusters = cpu_to_le32(sbi->free_clusters);
75 if (sbi->prev_free != -1)
76 fsinfo->next_cluster = cpu_to_le32(sbi->prev_free);
77 mark_buffer_dirty(bh);
78 }
79 brelse(bh);
80 }
81
82 /*
83 * fat_add_cluster tries to allocate a new cluster and adds it to the
84 * file represented by inode.
85 */
86 int fat_add_cluster(struct inode *inode)
87 {
88 struct super_block *sb = inode->i_sb;
89 struct msdos_sb_info *sbi = MSDOS_SB(sb);
90 int ret, count, limit, new_dclus, new_fclus, last;
91 int cluster_bits = sbi->cluster_bits;
92
93 /*
94 * We must locate the last cluster of the file to add this new
95 * one (new_dclus) to the end of the link list (the FAT).
96 *
97 * In order to confirm that the cluster chain is valid, we
98 * find out EOF first.
99 */
100 last = new_fclus = 0;
101 if (MSDOS_I(inode)->i_start) {
102 int ret, fclus, dclus;
103
104 ret = fat_get_cluster(inode, FAT_ENT_EOF, &fclus, &dclus);
105 if (ret < 0)
106 return ret;
107 new_fclus = fclus + 1;
108 last = dclus;
109 }
110
111 /* find free FAT entry */
112 lock_fat(sb);
113
114 if (sbi->free_clusters == 0) {
115 unlock_fat(sb);
116 return -ENOSPC;
117 }
118
119 limit = sbi->max_cluster;
120 new_dclus = sbi->prev_free + 1;
121 for (count = FAT_START_ENT; count < limit; count++, new_dclus++) {
122 new_dclus = new_dclus % limit;
123 if (new_dclus < FAT_START_ENT)
124 new_dclus = FAT_START_ENT;
125
126 ret = fat_access(sb, new_dclus, -1);
127 if (ret < 0) {
128 unlock_fat(sb);
129 return ret;
130 } else if (ret == FAT_ENT_FREE)
131 break;
132 }
133 if (count >= limit) {
134 sbi->free_clusters = 0;
135 unlock_fat(sb);
136 return -ENOSPC;
137 }
138
139 ret = fat_access(sb, new_dclus, FAT_ENT_EOF);
140 if (ret < 0) {
141 unlock_fat(sb);
142 return ret;
143 }
144
145 sbi->prev_free = new_dclus;
146 if (sbi->free_clusters != -1)
147 sbi->free_clusters--;
148 fat_clusters_flush(sb);
149
150 unlock_fat(sb);
151
152 /* add new one to the last of the cluster chain */
153 if (last) {
154 ret = fat_access(sb, last, new_dclus);
155 if (ret < 0)
156 return ret;
157 // fat_cache_add(inode, new_fclus, new_dclus);
158 } else {
159 MSDOS_I(inode)->i_start = new_dclus;
160 MSDOS_I(inode)->i_logstart = new_dclus;
161 mark_inode_dirty(inode);
162 }
163 if (new_fclus != (inode->i_blocks >> (cluster_bits - 9))) {
164 fat_fs_panic(sb, "clusters badly computed (%d != %lu)",
165 new_fclus, inode->i_blocks >> (cluster_bits - 9));
166 fat_cache_inval_inode(inode);
167 }
168 inode->i_blocks += sbi->cluster_size >> 9;
169
170 return new_dclus;
171 }
172
173 extern struct timezone sys_tz;
174
175 /* Linear day numbers of the respective 1sts in non-leap years. */
176 static int day_n[] = {
177 /* Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec */
178 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 0, 0, 0, 0
179 };
180
181 /* Convert a MS-DOS time/date pair to a UNIX date (seconds since 1 1 70). */
182 int date_dos2unix(unsigned short time, unsigned short date)
183 {
184 int month, year, secs;
185
186 /*
187 * first subtract and mask after that... Otherwise, if
188 * date == 0, bad things happen
189 */
190 month = ((date >> 5) - 1) & 15;
191 year = date >> 9;
192 secs = (time & 31)*2+60*((time >> 5) & 63)+(time >> 11)*3600+86400*
193 ((date & 31)-1+day_n[month]+(year/4)+year*365-((year & 3) == 0 &&
194 month < 2 ? 1 : 0)+3653);
195 /* days since 1.1.70 plus 80's leap day */
196 secs += sys_tz.tz_minuteswest*60;
197 return secs;
198 }
199
200 /* Convert linear UNIX date to a MS-DOS time/date pair. */
201 void fat_date_unix2dos(int unix_date, __le16 *time, __le16 *date)
202 {
203 int day, year, nl_day, month;
204
205 unix_date -= sys_tz.tz_minuteswest*60;
206
207 /* Jan 1 GMT 00:00:00 1980. But what about another time zone? */
208 if (unix_date < 315532800)
209 unix_date = 315532800;
210
211 *time = cpu_to_le16((unix_date % 60)/2+(((unix_date/60) % 60) << 5)+
212 (((unix_date/3600) % 24) << 11));
213 day = unix_date/86400-3652;
214 year = day/365;
215 if ((year+3)/4+365*year > day)
216 year--;
217 day -= (year+3)/4+365*year;
218 if (day == 59 && !(year & 3)) {
219 nl_day = day;
220 month = 2;
221 } else {
222 nl_day = (year & 3) || day <= 59 ? day : day-1;
223 for (month = 0; month < 12; month++) {
224 if (day_n[month] > nl_day)
225 break;
226 }
227 }
228 *date = cpu_to_le16(nl_day-day_n[month-1]+1+(month << 5)+(year << 9));
229 }
230
231 EXPORT_SYMBOL(fat_date_unix2dos);
232
233 /* Returns the inode number of the directory entry at offset pos. If bh is
234 non-NULL, it is brelse'd before. Pos is incremented. The buffer header is
235 returned in bh.
236 AV. Most often we do it item-by-item. Makes sense to optimize.
237 AV. OK, there we go: if both bh and de are non-NULL we assume that we just
238 AV. want the next entry (took one explicit de=NULL in vfat/namei.c).
239 AV. It's done in fat_get_entry() (inlined), here the slow case lives.
240 AV. Additionally, when we return -1 (i.e. reached the end of directory)
241 AV. we make bh NULL.
242 */
243
244 int fat__get_entry(struct inode *dir, loff_t *pos, struct buffer_head **bh,
245 struct msdos_dir_entry **de, loff_t *i_pos)
246 {
247 struct super_block *sb = dir->i_sb;
248 struct msdos_sb_info *sbi = MSDOS_SB(sb);
249 sector_t phys, iblock;
250 loff_t offset;
251 int err;
252
253 next:
254 offset = *pos;
255 if (*bh)
256 brelse(*bh);
257
258 *bh = NULL;
259 iblock = *pos >> sb->s_blocksize_bits;
260 err = fat_bmap(dir, iblock, &phys);
261 if (err || !phys)
262 return -1; /* beyond EOF or error */
263
264 *bh = sb_bread(sb, phys);
265 if (*bh == NULL) {
266 printk(KERN_ERR "FAT: Directory bread(block %llu) failed\n",
267 (unsigned long long)phys);
268 /* skip this block */
269 *pos = (iblock + 1) << sb->s_blocksize_bits;
270 goto next;
271 }
272
273 offset &= sb->s_blocksize - 1;
274 *pos += sizeof(struct msdos_dir_entry);
275 *de = (struct msdos_dir_entry *)((*bh)->b_data + offset);
276 *i_pos = ((loff_t)phys << sbi->dir_per_block_bits) + (offset >> MSDOS_DIR_BITS);
277
278 return 0;
279 }
280
281 EXPORT_SYMBOL(fat__get_entry);
282
|
This page was automatically generated by the
LXR engine.
|