panic() cleanup.
[minix.git] / servers / iso9660fs / read.c
blob789eade94a0e0952f5f9281f2a27b13c693820c1
1 #include "inc.h"
2 #include <minix/com.h>
3 #include <minix/vfsif.h>
4 #include <fcntl.h>
5 #include "buf.h"
7 PRIVATE char getdents_buf[GETDENTS_BUFSIZ];
10 /*===========================================================================*
11 * fs_read *
12 *===========================================================================*/
13 PUBLIC int fs_read(void) {
14 int r, chunk, block_size;
15 int nrbytes;
16 cp_grant_id_t gid;
17 off_t position, f_size, bytes_left;
18 unsigned int off, cum_io;
19 int completed;
20 struct dir_record *dir;
22 r = OK;
24 /* Try to get inode according to its index */
25 dir = get_dir_record(fs_m_in.REQ_INODE_NR);
26 if (dir == NULL) return(EINVAL); /* no inode found */
28 position = fs_m_in.REQ_SEEK_POS_LO;
29 nrbytes = (unsigned) fs_m_in.REQ_NBYTES; /* number of bytes to read */
30 block_size = v_pri.logical_block_size_l;
31 gid = fs_m_in.REQ_GRANT;
32 f_size = dir->d_file_size;
34 rdwt_err = OK; /* set to EIO if disk error occurs */
36 cum_io = 0;
37 /* Split the transfer into chunks that don't span two blocks. */
38 while (nrbytes != 0) {
39 off = (unsigned int) (position % block_size);
41 chunk = MIN(nrbytes, block_size - off);
42 if (chunk < 0) chunk = block_size - off;
44 bytes_left = f_size - position;
45 if (position >= f_size) break; /* we are beyond EOF */
46 if (chunk > bytes_left) chunk = (int) bytes_left;
48 /* Read or write 'chunk' bytes. */
49 r = read_chunk(dir, cvul64(position), off, chunk, (unsigned) nrbytes,
50 gid, cum_io, block_size, &completed);
52 if (r != OK) break; /* EOF reached */
53 if (rdwt_err < 0) break;
55 /* Update counters and pointers. */
56 nrbytes -= chunk; /* bytes yet to be read */
57 cum_io += chunk; /* bytes read so far */
58 position += chunk; /* position within the file */
61 fs_m_out.RES_SEEK_POS_LO = position;
63 if (rdwt_err != OK) r = rdwt_err; /* check for disk error */
64 if (rdwt_err == END_OF_FILE) r = OK;
66 fs_m_out.RES_NBYTES = cum_io; /*dir->d_file_size;*/
67 release_dir_record(dir);
69 return(r);
73 /*===========================================================================*
74 * fs_bread *
75 *===========================================================================*/
76 PUBLIC int fs_bread(void)
78 int r, rw_flag, chunk, block_size;
79 cp_grant_id_t gid;
80 int nrbytes;
81 u64_t position;
82 unsigned int off, cum_io;
83 mode_t mode_word;
84 int completed, r2 = OK;
85 struct dir_record *dir;
87 r = OK;
89 rw_flag = (fs_m_in.m_type == REQ_BREAD ? READING : WRITING);
90 gid = fs_m_in.REQ_GRANT;
91 position = make64(fs_m_in.REQ_SEEK_POS_LO, fs_m_in.REQ_SEEK_POS_HI);
92 nrbytes = (unsigned) fs_m_in.REQ_NBYTES;
93 block_size = v_pri.logical_block_size_l;
94 dir = v_pri.dir_rec_root;
96 if(rw_flag == WRITING) return (EIO); /* Not supported */
97 rdwt_err = OK; /* set to EIO if disk error occurs */
99 cum_io = 0;
100 /* Split the transfer into chunks that don't span two blocks. */
101 while (nrbytes != 0) {
102 off = rem64u(position, block_size); /* offset in blk*/
104 chunk = MIN(nrbytes, block_size - off);
105 if (chunk < 0) chunk = block_size - off;
107 /* Read 'chunk' bytes. */
108 r = read_chunk(dir, position, off, chunk, (unsigned) nrbytes,
109 gid, cum_io, block_size, &completed);
111 if (r != OK) break; /* EOF reached */
112 if (rdwt_err < 0) break;
114 /* Update counters and pointers. */
115 nrbytes -= chunk; /* bytes yet to be read */
116 cum_io += chunk; /* bytes read so far */
117 position= add64ul(position, chunk); /* position within the file */
120 fs_m_out.RES_SEEK_POS_LO = ex64lo(position);
121 fs_m_out.RES_SEEK_POS_HI = ex64hi(position);
123 if (rdwt_err != OK) r = rdwt_err; /* check for disk error */
124 if (rdwt_err == END_OF_FILE) r = OK;
126 fs_m_out.RES_NBYTES = cum_io;
128 return(r);
132 /*===========================================================================*
133 * fs_getdents *
134 *===========================================================================*/
135 PUBLIC int fs_getdents(void) {
136 struct dir_record *dir;
137 ino_t ino;
138 cp_grant_id_t gid;
139 size_t block_size;
140 off_t pos, block_pos, block, cur_pos, tmpbuf_offset, userbuf_off;
141 struct buf *bp;
142 struct dir_record *dir_tmp;
143 struct dirent *dirp;
144 int r,done,o,len,reclen;
145 char *cp;
146 char name[NAME_MAX + 1];
147 char name_old[NAME_MAX + 1];
149 /* Initialize the tmp arrays */
150 memset(name,'\0',NAME_MAX);
151 memset(name_old,'\0',NAME_MAX);
153 /* Get input parameters */
154 ino = fs_m_in.REQ_INODE_NR;
155 gid = fs_m_in.REQ_GRANT;
156 pos = fs_m_in.REQ_SEEK_POS_LO;
158 block_size = v_pri.logical_block_size_l;
159 cur_pos = pos; /* The current position */
160 tmpbuf_offset = 0;
161 userbuf_off = 0;
162 memset(getdents_buf, '\0', GETDENTS_BUFSIZ); /* Avoid leaking any data */
164 if ((dir = get_dir_record(ino)) == NULL) return(EINVAL);
166 block = dir->loc_extent_l; /* First block of the directory */
167 block += pos / block_size; /* Shift to the block where start to read */
168 done = FALSE;
170 while (cur_pos<dir->d_file_size) {
171 bp = get_block(block); /* Get physical block */
173 if (bp == NIL_BUF) {
174 release_dir_record(dir);
175 return(EINVAL);
178 block_pos = cur_pos % block_size; /* Position where to start read */
180 while (block_pos < block_size) {
181 dir_tmp = get_free_dir_record();
182 create_dir_record(dir_tmp,bp->b_data + block_pos,
183 block*block_size + block_pos);
184 if (dir_tmp->length == 0) { /* EOF. I exit and return 0s */
185 block_pos = block_size;
186 done = TRUE;
187 release_dir_record(dir_tmp);
188 } else { /* The dir record is valid. Copy data... */
189 if (dir_tmp->file_id[0] == 0) strcpy(name,".");
190 else if (dir_tmp->file_id[0] == 1) strcpy(name,"..");
191 else {
192 /* Extract the name from the field file_id */
193 strncpy(name, dir_tmp->file_id,
194 dir_tmp->length_file_id);
195 name[dir_tmp->length_file_id] = 0;
197 /* Tidy up file name */
198 cp = memchr(name, ';', NAME_MAX);
199 if (cp != NULL) name[cp - name] = 0;
201 /*If no file extension, then remove final '.'*/
202 if (name[strlen(name) - 1] == '.')
203 name[strlen(name) - 1] = '\0';
206 if (strcmp(name_old, name) == 0) {
207 cur_pos += dir_tmp->length;
208 release_dir_record(dir_tmp);
209 continue;
212 strcpy(name_old,name);
214 /* Compute the length of the name */
215 cp = memchr(name, '\0', NAME_MAX);
216 if (cp == NULL) len = NAME_MAX;
217 else len= cp - name;
219 /* Compute record length */
220 reclen = offsetof(struct dirent, d_name) + len + 1;
221 o = (reclen % sizeof(long));
222 if (o != 0)
223 reclen += sizeof(long) - o;
225 /* If the new record does not fit, then copy the buffer
226 * and start from the beginning. */
227 if (tmpbuf_offset + reclen > GETDENTS_BUFSIZ) {
228 r = sys_safecopyto(FS_PROC_NR, gid, userbuf_off,
229 (vir_bytes)getdents_buf, tmpbuf_offset, D);
231 if (r != OK)
232 panic("fs_getdents: sys_safecopyto failed: %d", r);
233 userbuf_off += tmpbuf_offset;
234 tmpbuf_offset= 0;
237 /* The standard data structure is created using the
238 * data in the buffer. */
239 dirp = (struct dirent *) &getdents_buf[tmpbuf_offset];
240 dirp->d_ino = (ino_t)(bp->b_data + block_pos);
241 dirp->d_off= cur_pos;
242 dirp->d_reclen= reclen;
243 memcpy(dirp->d_name, name, len);
244 dirp->d_name[len]= '\0';
245 tmpbuf_offset += reclen;
247 cur_pos += dir_tmp->length;
248 release_dir_record(dir_tmp);
251 block_pos += dir_tmp->length;
254 put_block(bp); /* release the block */
255 if (done == TRUE) break;
257 cur_pos += block_size - cur_pos;
258 block++; /* read the next one */
261 if (tmpbuf_offset != 0) {
262 r = sys_safecopyto(FS_PROC_NR, gid, userbuf_off,
263 (vir_bytes) getdents_buf, tmpbuf_offset, D);
264 if (r != OK)
265 panic("fs_getdents: sys_safecopyto failed: %d", r);
267 userbuf_off += tmpbuf_offset;
270 fs_m_out.RES_NBYTES = userbuf_off;
271 fs_m_out.RES_SEEK_POS_LO = cur_pos;
273 release_dir_record(dir); /* release the inode */
274 return(OK);
278 /*===========================================================================*
279 * read_chunk *
280 *===========================================================================*/
281 PUBLIC int read_chunk(dir, position, off, chunk, left, gid, buf_off, block_size, completed)
282 register struct dir_record *dir;/* pointer to inode for file to be rd/wr */
283 u64_t position; /* position within file to read or write */
284 unsigned off; /* off within the current block */
285 int chunk; /* number of bytes to read or write */
286 unsigned left; /* max number of bytes wanted after position */
287 cp_grant_id_t gid; /* grant */
288 unsigned buf_off; /* offset in grant */
289 int block_size; /* block size of FS operating on */
290 int *completed; /* number of bytes copied */
293 register struct buf *bp;
294 register int r = OK;
295 int n;
296 block_t b;
297 dev_t dev;
298 int file_unit, rel_block, offset;
300 *completed = 0;
302 if ((ex64lo(position) <= dir->d_file_size) &&
303 (ex64lo(position) > dir->data_length_l)) {
304 while ((dir->d_next != NULL) && (ex64lo(position) > dir->data_length_l)) {
305 position = sub64ul(position, dir->data_length_l);
306 dir = dir->d_next;
310 if (dir->inter_gap_size != 0) {
311 rel_block = div64u(position, block_size);
312 file_unit = rel_block / dir->data_length_l;
313 offset = rel_block % dir->file_unit_size;
314 b = dir->loc_extent_l + (dir->file_unit_size +
315 dir->inter_gap_size) * file_unit + offset;
316 } else {
317 b = dir->loc_extent_l + div64u(position, block_size); /* Physical position
318 * to read. */
321 bp = get_block(b);
323 /* In all cases, bp now points to a valid buffer. */
324 if (bp == NIL_BUF) {
325 panic("bp not valid in rw_chunk; this can't happen");
328 r = sys_safecopyto(FS_PROC_NR, gid, buf_off,
329 (vir_bytes) (bp->b_data+off), (phys_bytes) chunk, D);
331 put_block(bp);
333 return(r);