fileio.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * fileio.c --- Simple file I/O routines
  4. *
  5. * Copyright (C) 1997 Theodore Ts'o.
  6. *
  7. * %Begin-Header%
  8. * This file may be redistributed under the terms of the GNU Public
  9. * License.
  10. * %End-Header%
  11. */
  12. #include <stdio.h>
  13. #include <string.h>
  14. #if HAVE_UNISTD_H
  15. #include <unistd.h>
  16. #endif
  17. #include "ext2_fs.h"
  18. #include "ext2fs.h"
  19. struct ext2_file {
  20. errcode_t magic;
  21. ext2_filsys fs;
  22. ext2_ino_t ino;
  23. struct ext2_inode inode;
  24. int flags;
  25. __u64 pos;
  26. blk_t blockno;
  27. blk_t physblock;
  28. char *buf;
  29. };
  30. #define BMAP_BUFFER (file->buf + fs->blocksize)
  31. errcode_t ext2fs_file_open2(ext2_filsys fs, ext2_ino_t ino,
  32. struct ext2_inode *inode,
  33. int flags, ext2_file_t *ret)
  34. {
  35. ext2_file_t file;
  36. errcode_t retval;
  37. /*
  38. * Don't let caller create or open a file for writing if the
  39. * filesystem is read-only.
  40. */
  41. if ((flags & (EXT2_FILE_WRITE | EXT2_FILE_CREATE)) &&
  42. !(fs->flags & EXT2_FLAG_RW))
  43. return EXT2_ET_RO_FILSYS;
  44. retval = ext2fs_get_mem(sizeof(struct ext2_file), &file);
  45. if (retval)
  46. return retval;
  47. memset(file, 0, sizeof(struct ext2_file));
  48. file->magic = EXT2_ET_MAGIC_EXT2_FILE;
  49. file->fs = fs;
  50. file->ino = ino;
  51. file->flags = flags & EXT2_FILE_MASK;
  52. if (inode) {
  53. memcpy(&file->inode, inode, sizeof(struct ext2_inode));
  54. } else {
  55. retval = ext2fs_read_inode(fs, ino, &file->inode);
  56. if (retval)
  57. goto fail;
  58. }
  59. retval = ext2fs_get_mem(fs->blocksize * 3, &file->buf);
  60. if (retval)
  61. goto fail;
  62. *ret = file;
  63. return 0;
  64. fail:
  65. ext2fs_free_mem(&file->buf);
  66. ext2fs_free_mem(&file);
  67. return retval;
  68. }
  69. errcode_t ext2fs_file_open(ext2_filsys fs, ext2_ino_t ino,
  70. int flags, ext2_file_t *ret)
  71. {
  72. return ext2fs_file_open2(fs, ino, NULL, flags, ret);
  73. }
  74. /*
  75. * This function returns the filesystem handle of a file from the structure
  76. */
  77. ext2_filsys ext2fs_file_get_fs(ext2_file_t file)
  78. {
  79. if (file->magic != EXT2_ET_MAGIC_EXT2_FILE)
  80. return 0;
  81. return file->fs;
  82. }
  83. /*
  84. * This function flushes the dirty block buffer out to disk if
  85. * necessary.
  86. */
  87. errcode_t ext2fs_file_flush(ext2_file_t file)
  88. {
  89. errcode_t retval;
  90. ext2_filsys fs;
  91. EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
  92. fs = file->fs;
  93. if (!(file->flags & EXT2_FILE_BUF_VALID) ||
  94. !(file->flags & EXT2_FILE_BUF_DIRTY))
  95. return 0;
  96. /*
  97. * OK, the physical block hasn't been allocated yet.
  98. * Allocate it.
  99. */
  100. if (!file->physblock) {
  101. retval = ext2fs_bmap(fs, file->ino, &file->inode,
  102. BMAP_BUFFER, file->ino ? BMAP_ALLOC : 0,
  103. file->blockno, &file->physblock);
  104. if (retval)
  105. return retval;
  106. }
  107. retval = io_channel_write_blk(fs->io, file->physblock,
  108. 1, file->buf);
  109. if (retval)
  110. return retval;
  111. file->flags &= ~EXT2_FILE_BUF_DIRTY;
  112. return retval;
  113. }
  114. /*
  115. * This function synchronizes the file's block buffer and the current
  116. * file position, possibly invalidating block buffer if necessary
  117. */
  118. static errcode_t sync_buffer_position(ext2_file_t file)
  119. {
  120. blk_t b;
  121. errcode_t retval;
  122. b = file->pos / file->fs->blocksize;
  123. if (b != file->blockno) {
  124. retval = ext2fs_file_flush(file);
  125. if (retval)
  126. return retval;
  127. file->flags &= ~EXT2_FILE_BUF_VALID;
  128. }
  129. file->blockno = b;
  130. return 0;
  131. }
  132. /*
  133. * This function loads the file's block buffer with valid data from
  134. * the disk as necessary.
  135. *
  136. * If dontfill is true, then skip initializing the buffer since we're
  137. * going to be replacing its entire contents anyway. If set, then the
  138. * function basically only sets file->physblock and EXT2_FILE_BUF_VALID
  139. */
  140. #define DONTFILL 1
  141. static errcode_t load_buffer(ext2_file_t file, int dontfill)
  142. {
  143. ext2_filsys fs = file->fs;
  144. errcode_t retval;
  145. if (!(file->flags & EXT2_FILE_BUF_VALID)) {
  146. retval = ext2fs_bmap(fs, file->ino, &file->inode,
  147. BMAP_BUFFER, 0, file->blockno,
  148. &file->physblock);
  149. if (retval)
  150. return retval;
  151. if (!dontfill) {
  152. if (file->physblock) {
  153. retval = io_channel_read_blk(fs->io,
  154. file->physblock,
  155. 1, file->buf);
  156. if (retval)
  157. return retval;
  158. } else
  159. memset(file->buf, 0, fs->blocksize);
  160. }
  161. file->flags |= EXT2_FILE_BUF_VALID;
  162. }
  163. return 0;
  164. }
  165. errcode_t ext2fs_file_close(ext2_file_t file)
  166. {
  167. errcode_t retval;
  168. EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
  169. retval = ext2fs_file_flush(file);
  170. ext2fs_free_mem(&file->buf);
  171. ext2fs_free_mem(&file);
  172. return retval;
  173. }
  174. errcode_t ext2fs_file_read(ext2_file_t file, void *buf,
  175. unsigned int wanted, unsigned int *got)
  176. {
  177. ext2_filsys fs;
  178. errcode_t retval = 0;
  179. unsigned int start, c, count = 0;
  180. __u64 left;
  181. char *ptr = (char *) buf;
  182. EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
  183. fs = file->fs;
  184. while ((file->pos < EXT2_I_SIZE(&file->inode)) && (wanted > 0)) {
  185. retval = sync_buffer_position(file);
  186. if (retval)
  187. goto fail;
  188. retval = load_buffer(file, 0);
  189. if (retval)
  190. goto fail;
  191. start = file->pos % fs->blocksize;
  192. c = fs->blocksize - start;
  193. if (c > wanted)
  194. c = wanted;
  195. left = EXT2_I_SIZE(&file->inode) - file->pos ;
  196. if (c > left)
  197. c = left;
  198. memcpy(ptr, file->buf+start, c);
  199. file->pos += c;
  200. ptr += c;
  201. count += c;
  202. wanted -= c;
  203. }
  204. fail:
  205. if (got)
  206. *got = count;
  207. return retval;
  208. }
  209. errcode_t ext2fs_file_write(ext2_file_t file, const void *buf,
  210. unsigned int nbytes, unsigned int *written)
  211. {
  212. ext2_filsys fs;
  213. errcode_t retval = 0;
  214. unsigned int start, c, count = 0;
  215. const char *ptr = (const char *) buf;
  216. EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
  217. fs = file->fs;
  218. if (!(file->flags & EXT2_FILE_WRITE))
  219. return EXT2_ET_FILE_RO;
  220. while (nbytes > 0) {
  221. retval = sync_buffer_position(file);
  222. if (retval)
  223. goto fail;
  224. start = file->pos % fs->blocksize;
  225. c = fs->blocksize - start;
  226. if (c > nbytes)
  227. c = nbytes;
  228. /*
  229. * We only need to do a read-modify-update cycle if
  230. * we're doing a partial write.
  231. */
  232. retval = load_buffer(file, (c == fs->blocksize));
  233. if (retval)
  234. goto fail;
  235. file->flags |= EXT2_FILE_BUF_DIRTY;
  236. memcpy(file->buf+start, ptr, c);
  237. file->pos += c;
  238. ptr += c;
  239. count += c;
  240. nbytes -= c;
  241. }
  242. fail:
  243. if (written)
  244. *written = count;
  245. return retval;
  246. }
  247. errcode_t ext2fs_file_llseek(ext2_file_t file, __u64 offset,
  248. int whence, __u64 *ret_pos)
  249. {
  250. EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
  251. if (whence == EXT2_SEEK_SET)
  252. file->pos = offset;
  253. else if (whence == EXT2_SEEK_CUR)
  254. file->pos += offset;
  255. else if (whence == EXT2_SEEK_END)
  256. file->pos = EXT2_I_SIZE(&file->inode) + offset;
  257. else
  258. return EXT2_ET_INVALID_ARGUMENT;
  259. if (ret_pos)
  260. *ret_pos = file->pos;
  261. return 0;
  262. }
  263. errcode_t ext2fs_file_lseek(ext2_file_t file, ext2_off_t offset,
  264. int whence, ext2_off_t *ret_pos)
  265. {
  266. __u64 loffset, ret_loffset;
  267. errcode_t retval;
  268. loffset = offset;
  269. retval = ext2fs_file_llseek(file, loffset, whence, &ret_loffset);
  270. if (ret_pos)
  271. *ret_pos = (ext2_off_t) ret_loffset;
  272. return retval;
  273. }
  274. /*
  275. * This function returns the size of the file, according to the inode
  276. */
  277. errcode_t ext2fs_file_get_lsize(ext2_file_t file, __u64 *ret_size)
  278. {
  279. if (file->magic != EXT2_ET_MAGIC_EXT2_FILE)
  280. return EXT2_ET_MAGIC_EXT2_FILE;
  281. *ret_size = EXT2_I_SIZE(&file->inode);
  282. return 0;
  283. }
  284. /*
  285. * This function returns the size of the file, according to the inode
  286. */
  287. ext2_off_t ext2fs_file_get_size(ext2_file_t file)
  288. {
  289. __u64 size;
  290. if (ext2fs_file_get_lsize(file, &size))
  291. return 0;
  292. if ((size >> 32) != 0)
  293. return 0;
  294. return size;
  295. }
  296. /*
  297. * This function sets the size of the file, truncating it if necessary
  298. *
  299. * XXX still need to call truncate
  300. */
  301. errcode_t ext2fs_file_set_size(ext2_file_t file, ext2_off_t size)
  302. {
  303. errcode_t retval;
  304. EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
  305. file->inode.i_size = size;
  306. file->inode.i_size_high = 0;
  307. if (file->ino) {
  308. retval = ext2fs_write_inode(file->fs, file->ino, &file->inode);
  309. if (retval)
  310. return retval;
  311. }
  312. /*
  313. * XXX truncate inode if necessary
  314. */
  315. return 0;
  316. }