- Linus: drop support for old-style Makefiles entirely. Big.
[davej-history.git] / fs / isofs / rock.c
blob4ac4bc0ced4363d44ca8c74518a29399a25555ba
1 /*
2 * linux/fs/isofs/rock.c
4 * (C) 1992, 1993 Eric Youngdale
6 * Rock Ridge Extensions to iso9660
7 */
9 #include <linux/stat.h>
10 #include <linux/sched.h>
11 #include <linux/iso_fs.h>
12 #include <linux/string.h>
13 #include <linux/mm.h>
14 #include <linux/malloc.h>
15 #include <linux/pagemap.h>
16 #include <linux/smp_lock.h>
18 #include "rock.h"
20 /* These functions are designed to read the system areas of a directory record
21 * and extract relevant information. There are different functions provided
22 * depending upon what information we need at the time. One function fills
23 * out an inode structure, a second one extracts a filename, a third one
24 * returns a symbolic link name, and a fourth one returns the extent number
25 * for the file. */
27 #define SIG(A,B) ((A << 8) | B)
30 /* This is a way of ensuring that we have something in the system
31 use fields that is compatible with Rock Ridge */
32 #define CHECK_SP(FAIL) \
33 if(rr->u.SP.magic[0] != 0xbe) FAIL; \
34 if(rr->u.SP.magic[1] != 0xef) FAIL;
36 /* We define a series of macros because each function must do exactly the
37 same thing in certain places. We use the macros to ensure that everything
38 is done correctly */
40 #define CONTINUE_DECLS \
41 int cont_extent = 0, cont_offset = 0, cont_size = 0; \
42 void * buffer = 0
44 #define CHECK_CE \
45 {cont_extent = isonum_733(rr->u.CE.extent); \
46 cont_offset = isonum_733(rr->u.CE.offset); \
47 cont_size = isonum_733(rr->u.CE.size);}
49 #define SETUP_ROCK_RIDGE(DE,CHR,LEN) \
50 {LEN= sizeof(struct iso_directory_record) + DE->name_len[0]; \
51 if(LEN & 1) LEN++; \
52 CHR = ((unsigned char *) DE) + LEN; \
53 LEN = *((unsigned char *) DE) - LEN;}
55 #define MAYBE_CONTINUE(LABEL,DEV) \
56 {if (buffer) kfree(buffer); \
57 if (cont_extent){ \
58 int block, offset, offset1; \
59 struct buffer_head * pbh; \
60 buffer = kmalloc(cont_size,GFP_KERNEL); \
61 if (!buffer) goto out; \
62 block = cont_extent; \
63 offset = cont_offset; \
64 offset1 = 0; \
65 pbh = bread(DEV->i_dev, block, ISOFS_BUFFER_SIZE(DEV)); \
66 if(pbh){ \
67 memcpy(buffer + offset1, pbh->b_data + offset, cont_size - offset1); \
68 brelse(pbh); \
69 chr = (unsigned char *) buffer; \
70 len = cont_size; \
71 cont_extent = 0; \
72 cont_size = 0; \
73 cont_offset = 0; \
74 goto LABEL; \
75 } \
76 printk("Unable to read rock-ridge attributes\n"); \
79 /* This is the inner layer of the get filename routine, and is called
80 for each system area and continuation record related to the file */
82 int find_rock_ridge_relocation(struct iso_directory_record * de,
83 struct inode * inode) {
84 int flag;
85 int len;
86 int retval;
87 unsigned char * chr;
88 CONTINUE_DECLS;
89 flag = 0;
91 /* If this is a '..' then we are looking for the parent, otherwise we
92 are looking for the child */
94 if (de->name[0]==1 && de->name_len[0]==1) flag = 1;
95 /* Return value if we do not find appropriate record. */
96 retval = isonum_733 (de->extent);
98 if (!inode->i_sb->u.isofs_sb.s_rock) return retval;
100 SETUP_ROCK_RIDGE(de, chr, len);
101 repeat:
103 int rrflag, sig;
104 struct rock_ridge * rr;
106 while (len > 1){ /* There may be one byte for padding somewhere */
107 rr = (struct rock_ridge *) chr;
108 if (rr->len == 0) goto out; /* Something got screwed up here */
109 sig = (chr[0] << 8) + chr[1];
110 chr += rr->len;
111 len -= rr->len;
113 switch(sig){
114 case SIG('R','R'):
115 rrflag = rr->u.RR.flags[0];
116 if (flag && !(rrflag & RR_PL)) goto out;
117 if (!flag && !(rrflag & RR_CL)) goto out;
118 break;
119 case SIG('S','P'):
120 CHECK_SP(goto out);
121 break;
122 case SIG('C','L'):
123 if (flag == 0) {
124 retval = isonum_733(rr->u.CL.location);
125 goto out;
127 break;
128 case SIG('P','L'):
129 if (flag != 0) {
130 retval = isonum_733(rr->u.PL.location);
131 goto out;
133 break;
134 case SIG('C','E'):
135 CHECK_CE; /* This tells is if there is a continuation record */
136 break;
137 default:
138 break;
142 MAYBE_CONTINUE(repeat, inode);
143 return retval;
144 out:
145 if(buffer) kfree(buffer);
146 return retval;
149 /* return length of name field; 0: not found, -1: to be ignored */
150 int get_rock_ridge_filename(struct iso_directory_record * de,
151 char * retname, struct inode * inode)
153 int len;
154 unsigned char * chr;
155 CONTINUE_DECLS;
156 int retnamlen = 0, truncate=0;
158 if (!inode->i_sb->u.isofs_sb.s_rock) return 0;
159 *retname = 0;
161 SETUP_ROCK_RIDGE(de, chr, len);
162 repeat:
164 struct rock_ridge * rr;
165 int sig;
167 while (len > 1){ /* There may be one byte for padding somewhere */
168 rr = (struct rock_ridge *) chr;
169 if (rr->len == 0) goto out; /* Something got screwed up here */
170 sig = (chr[0] << 8) + chr[1];
171 chr += rr->len;
172 len -= rr->len;
174 switch(sig){
175 case SIG('R','R'):
176 if((rr->u.RR.flags[0] & RR_NM) == 0) goto out;
177 break;
178 case SIG('S','P'):
179 CHECK_SP(goto out);
180 break;
181 case SIG('C','E'):
182 CHECK_CE;
183 break;
184 case SIG('N','M'):
185 if (truncate) break;
187 * If the flags are 2 or 4, this indicates '.' or '..'.
188 * We don't want to do anything with this, because it
189 * screws up the code that calls us. We don't really
190 * care anyways, since we can just use the non-RR
191 * name.
193 if (rr->u.NM.flags & 6) {
194 break;
197 if (rr->u.NM.flags & ~1) {
198 printk("Unsupported NM flag settings (%d)\n",rr->u.NM.flags);
199 break;
201 if((strlen(retname) + rr->len - 5) >= 254) {
202 truncate = 1;
203 break;
205 strncat(retname, rr->u.NM.name, rr->len - 5);
206 retnamlen += rr->len - 5;
207 break;
208 case SIG('R','E'):
209 if (buffer) kfree(buffer);
210 return -1;
211 default:
212 break;
216 MAYBE_CONTINUE(repeat,inode);
217 return retnamlen; /* If 0, this file did not have a NM field */
218 out:
219 if(buffer) kfree(buffer);
220 return 0;
223 int parse_rock_ridge_inode(struct iso_directory_record * de,
224 struct inode * inode){
225 int len;
226 unsigned char * chr;
227 int symlink_len = 0;
228 CONTINUE_DECLS;
230 if (!inode->i_sb->u.isofs_sb.s_rock) return 0;
232 SETUP_ROCK_RIDGE(de, chr, len);
233 repeat:
235 int cnt, sig;
236 struct inode * reloc;
237 struct rock_ridge * rr;
238 int rootflag;
240 while (len > 1){ /* There may be one byte for padding somewhere */
241 rr = (struct rock_ridge *) chr;
242 if (rr->len == 0) goto out; /* Something got screwed up here */
243 sig = (chr[0] << 8) + chr[1];
244 chr += rr->len;
245 len -= rr->len;
247 switch(sig){
248 case SIG('R','R'):
249 if((rr->u.RR.flags[0] &
250 (RR_PX | RR_TF | RR_SL | RR_CL)) == 0) goto out;
251 break;
252 case SIG('S','P'):
253 CHECK_SP(goto out);
254 break;
255 case SIG('C','E'):
256 CHECK_CE;
257 break;
258 case SIG('E','R'):
259 inode->i_sb->u.isofs_sb.s_rock = 1;
260 printk(KERN_DEBUG "ISO 9660 Extensions: ");
261 { int p;
262 for(p=0;p<rr->u.ER.len_id;p++) printk("%c",rr->u.ER.data[p]);
264 printk("\n");
265 break;
266 case SIG('P','X'):
267 inode->i_mode = isonum_733(rr->u.PX.mode);
268 inode->i_nlink = isonum_733(rr->u.PX.n_links);
269 inode->i_uid = isonum_733(rr->u.PX.uid);
270 inode->i_gid = isonum_733(rr->u.PX.gid);
271 break;
272 case SIG('P','N'):
273 { int high, low;
274 high = isonum_733(rr->u.PN.dev_high);
275 low = isonum_733(rr->u.PN.dev_low);
277 * The Rock Ridge standard specifies that if sizeof(dev_t) <= 4,
278 * then the high field is unused, and the device number is completely
279 * stored in the low field. Some writers may ignore this subtlety,
280 * and as a result we test to see if the entire device number is
281 * stored in the low field, and use that.
283 if((low & ~0xff) && high == 0) {
284 inode->i_rdev = MKDEV(low >> 8, low & 0xff);
285 } else {
286 inode->i_rdev = MKDEV(high, low);
289 break;
290 case SIG('T','F'):
291 /* Some RRIP writers incorrectly place ctime in the TF_CREATE field.
292 Try to handle this correctly for either case. */
293 cnt = 0; /* Rock ridge never appears on a High Sierra disk */
294 if(rr->u.TF.flags & TF_CREATE)
295 inode->i_ctime = iso_date(rr->u.TF.times[cnt++].time, 0);
296 if(rr->u.TF.flags & TF_MODIFY)
297 inode->i_mtime = iso_date(rr->u.TF.times[cnt++].time, 0);
298 if(rr->u.TF.flags & TF_ACCESS)
299 inode->i_atime = iso_date(rr->u.TF.times[cnt++].time, 0);
300 if(rr->u.TF.flags & TF_ATTRIBUTES)
301 inode->i_ctime = iso_date(rr->u.TF.times[cnt++].time, 0);
302 break;
303 case SIG('S','L'):
304 {int slen;
305 struct SL_component * slp;
306 struct SL_component * oldslp;
307 slen = rr->len - 5;
308 slp = &rr->u.SL.link;
309 inode->i_size = symlink_len;
310 while (slen > 1){
311 rootflag = 0;
312 switch(slp->flags &~1){
313 case 0:
314 inode->i_size += slp->len;
315 break;
316 case 2:
317 inode->i_size += 1;
318 break;
319 case 4:
320 inode->i_size += 2;
321 break;
322 case 8:
323 rootflag = 1;
324 inode->i_size += 1;
325 break;
326 default:
327 printk("Symlink component flag not implemented\n");
329 slen -= slp->len + 2;
330 oldslp = slp;
331 slp = (struct SL_component *) (((char *) slp) + slp->len + 2);
333 if(slen < 2) {
334 if( ((rr->u.SL.flags & 1) != 0)
335 && ((oldslp->flags & 1) == 0) ) inode->i_size += 1;
336 break;
340 * If this component record isn't continued, then append a '/'.
342 if (!rootflag && (oldslp->flags & 1) == 0)
343 inode->i_size += 1;
346 symlink_len = inode->i_size;
347 break;
348 case SIG('R','E'):
349 printk(KERN_WARNING "Attempt to read inode for relocated directory\n");
350 goto out;
351 case SIG('C','L'):
352 inode->u.isofs_i.i_first_extent = isonum_733(rr->u.CL.location);
353 reloc = iget(inode->i_sb,
354 (inode->u.isofs_i.i_first_extent <<
355 inode -> i_sb -> u.isofs_sb.s_log_zone_size));
356 if (!reloc)
357 goto out;
358 inode->i_mode = reloc->i_mode;
359 inode->i_nlink = reloc->i_nlink;
360 inode->i_uid = reloc->i_uid;
361 inode->i_gid = reloc->i_gid;
362 inode->i_rdev = reloc->i_rdev;
363 inode->i_size = reloc->i_size;
364 inode->i_atime = reloc->i_atime;
365 inode->i_ctime = reloc->i_ctime;
366 inode->i_mtime = reloc->i_mtime;
367 iput(reloc);
368 break;
369 default:
370 break;
374 MAYBE_CONTINUE(repeat,inode);
375 return 0;
376 out:
377 if(buffer) kfree(buffer);
378 return 0;
381 static char *get_symlink_chunk(char *rpnt, struct rock_ridge *rr)
383 int slen;
384 int rootflag;
385 struct SL_component *oldslp;
386 struct SL_component *slp;
387 slen = rr->len - 5;
388 slp = &rr->u.SL.link;
389 while (slen > 1) {
390 rootflag = 0;
391 switch (slp->flags & ~1) {
392 case 0:
393 memcpy(rpnt, slp->text, slp->len);
394 rpnt+=slp->len;
395 break;
396 case 4:
397 *rpnt++='.';
398 /* fallthru */
399 case 2:
400 *rpnt++='.';
401 break;
402 case 8:
403 rootflag = 1;
404 *rpnt++='/';
405 break;
406 default:
407 printk("Symlink component flag not implemented (%d)\n",
408 slp->flags);
410 slen -= slp->len + 2;
411 oldslp = slp;
412 slp = (struct SL_component *) ((char *) slp + slp->len + 2);
414 if (slen < 2) {
416 * If there is another SL record, and this component
417 * record isn't continued, then add a slash.
419 if ((rr->u.SL.flags & 1) && !(oldslp->flags & 1))
420 *rpnt++='/';
421 break;
425 * If this component record isn't continued, then append a '/'.
427 if (!rootflag && !(oldslp->flags & 1))
428 *rpnt++='/';
431 return rpnt;
435 /* readpage() for symlinks: reads symlink contents into the page and either
436 makes it uptodate and returns 0 or returns error (-EIO) */
438 static int rock_ridge_symlink_readpage(struct file *file, struct page *page)
440 struct inode *inode = (struct inode*)page->mapping->host;
441 char *link = kmap(page);
442 unsigned long bufsize = ISOFS_BUFFER_SIZE(inode);
443 unsigned char bufbits = ISOFS_BUFFER_BITS(inode);
444 struct buffer_head *bh;
445 char *rpnt = link;
446 unsigned char *pnt;
447 struct iso_directory_record *raw_inode;
448 CONTINUE_DECLS;
449 int block;
450 int sig;
451 int len;
452 unsigned char *chr;
453 struct rock_ridge *rr;
455 if (!inode->i_sb->u.isofs_sb.s_rock)
456 panic ("Cannot have symlink with high sierra variant of iso filesystem\n");
458 block = inode->i_ino >> bufbits;
459 lock_kernel();
460 bh = bread(inode->i_dev, block, bufsize);
461 if (!bh)
462 goto out_noread;
464 pnt = (unsigned char *) bh->b_data + (inode->i_ino & (bufsize - 1));
466 raw_inode = (struct iso_directory_record *) pnt;
469 * If we go past the end of the buffer, there is some sort of error.
471 if ((inode->i_ino & (bufsize - 1)) + *pnt > bufsize)
472 goto out_bad_span;
474 /* Now test for possible Rock Ridge extensions which will override
475 some of these numbers in the inode structure. */
477 SETUP_ROCK_RIDGE(raw_inode, chr, len);
479 repeat:
480 while (len > 1) { /* There may be one byte for padding somewhere */
481 rr = (struct rock_ridge *) chr;
482 if (rr->len == 0)
483 goto out; /* Something got screwed up here */
484 sig = (chr[0] << 8) + chr[1];
485 chr += rr->len;
486 len -= rr->len;
488 switch (sig) {
489 case SIG('R', 'R'):
490 if ((rr->u.RR.flags[0] & RR_SL) == 0)
491 goto out;
492 break;
493 case SIG('S', 'P'):
494 CHECK_SP(goto out);
495 break;
496 case SIG('S', 'L'):
497 rpnt = get_symlink_chunk(rpnt, rr);
498 break;
499 case SIG('C', 'E'):
500 /* This tells is if there is a continuation record */
501 CHECK_CE;
502 default:
503 break;
506 MAYBE_CONTINUE(repeat, inode);
508 if (rpnt == link)
509 goto fail;
510 brelse(bh);
511 *rpnt = '\0';
512 unlock_kernel();
513 SetPageUptodate(page);
514 kunmap(page);
515 UnlockPage(page);
516 return 0;
518 /* error exit from macro */
519 out:
520 if (buffer)
521 kfree(buffer);
522 goto fail;
523 out_noread:
524 printk("unable to read i-node block");
525 goto fail;
526 out_bad_span:
527 printk("symlink spans iso9660 blocks\n");
528 fail:
529 brelse(bh);
530 unlock_kernel();
531 SetPageError(page);
532 kunmap(page);
533 UnlockPage(page);
534 return -EIO;
537 struct address_space_operations isofs_symlink_aops = {
538 readpage: rock_ridge_symlink_readpage