inode.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061
  1. /*
  2. * inode.c
  3. *
  4. * PURPOSE
  5. * Inode handling routines for the OSTA-UDF(tm) filesystem.
  6. *
  7. * COPYRIGHT
  8. * This file is distributed under the terms of the GNU General Public
  9. * License (GPL). Copies of the GPL can be obtained from:
  10. * ftp://prep.ai.mit.edu/pub/gnu/GPL
  11. * Each contributing author retains all rights to their own work.
  12. *
  13. * (C) 1998 Dave Boynton
  14. * (C) 1998-2004 Ben Fennema
  15. * (C) 1999-2000 Stelias Computing Inc
  16. *
  17. * HISTORY
  18. *
  19. * 10/04/98 dgb Added rudimentary directory functions
  20. * 10/07/98 Fully working udf_block_map! It works!
  21. * 11/25/98 bmap altered to better support extents
  22. * 12/06/98 blf partition support in udf_iget, udf_block_map
  23. * and udf_read_inode
  24. * 12/12/98 rewrote udf_block_map to handle next extents and descs across
  25. * block boundaries (which is not actually allowed)
  26. * 12/20/98 added support for strategy 4096
  27. * 03/07/99 rewrote udf_block_map (again)
  28. * New funcs, inode_bmap, udf_next_aext
  29. * 04/19/99 Support for writing device EA's for major/minor #
  30. */
  31. #include "udfdecl.h"
  32. #include <linux/mm.h>
  33. #include <linux/smp_lock.h>
  34. #include <linux/module.h>
  35. #include <linux/pagemap.h>
  36. #include <linux/buffer_head.h>
  37. #include <linux/writeback.h>
  38. #include <linux/slab.h>
  39. #include <linux/crc-itu-t.h>
  40. #include "udf_i.h"
  41. #include "udf_sb.h"
  42. MODULE_AUTHOR("Ben Fennema");
  43. MODULE_DESCRIPTION("Universal Disk Format Filesystem");
  44. MODULE_LICENSE("GPL");
  45. #define EXTENT_MERGE_SIZE 5
  46. static mode_t udf_convert_permissions(struct fileEntry *);
  47. static int udf_update_inode(struct inode *, int);
  48. static void udf_fill_inode(struct inode *, struct buffer_head *);
  49. static int udf_sync_inode(struct inode *inode);
  50. static int udf_alloc_i_data(struct inode *inode, size_t size);
  51. static struct buffer_head *inode_getblk(struct inode *, sector_t, int *,
  52. sector_t *, int *);
  53. static int8_t udf_insert_aext(struct inode *, struct extent_position,
  54. struct kernel_lb_addr, uint32_t);
  55. static void udf_split_extents(struct inode *, int *, int, int,
  56. struct kernel_long_ad[EXTENT_MERGE_SIZE], int *);
  57. static void udf_prealloc_extents(struct inode *, int, int,
  58. struct kernel_long_ad[EXTENT_MERGE_SIZE], int *);
  59. static void udf_merge_extents(struct inode *,
  60. struct kernel_long_ad[EXTENT_MERGE_SIZE], int *);
  61. static void udf_update_extents(struct inode *,
  62. struct kernel_long_ad[EXTENT_MERGE_SIZE], int, int,
  63. struct extent_position *);
  64. static int udf_get_block(struct inode *, sector_t, struct buffer_head *, int);
  65. void udf_evict_inode(struct inode *inode)
  66. {
  67. struct udf_inode_info *iinfo = UDF_I(inode);
  68. int want_delete = 0;
  69. truncate_inode_pages(&inode->i_data, 0);
  70. if (!inode->i_nlink && !is_bad_inode(inode)) {
  71. want_delete = 1;
  72. inode->i_size = 0;
  73. udf_truncate(inode);
  74. udf_update_inode(inode, IS_SYNC(inode));
  75. }
  76. invalidate_inode_buffers(inode);
  77. end_writeback(inode);
  78. if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB &&
  79. inode->i_size != iinfo->i_lenExtents) {
  80. printk(KERN_WARNING "UDF-fs (%s): Inode %lu (mode %o) has "
  81. "inode size %llu different from extent length %llu. "
  82. "Filesystem need not be standards compliant.\n",
  83. inode->i_sb->s_id, inode->i_ino, inode->i_mode,
  84. (unsigned long long)inode->i_size,
  85. (unsigned long long)iinfo->i_lenExtents);
  86. }
  87. kfree(iinfo->i_ext.i_data);
  88. iinfo->i_ext.i_data = NULL;
  89. if (want_delete) {
  90. lock_kernel();
  91. udf_free_inode(inode);
  92. unlock_kernel();
  93. }
  94. }
  95. static int udf_writepage(struct page *page, struct writeback_control *wbc)
  96. {
  97. return block_write_full_page(page, udf_get_block, wbc);
  98. }
  99. static int udf_readpage(struct file *file, struct page *page)
  100. {
  101. return block_read_full_page(page, udf_get_block);
  102. }
  103. static int udf_write_begin(struct file *file, struct address_space *mapping,
  104. loff_t pos, unsigned len, unsigned flags,
  105. struct page **pagep, void **fsdata)
  106. {
  107. int ret;
  108. ret = block_write_begin(mapping, pos, len, flags, pagep, udf_get_block);
  109. if (unlikely(ret)) {
  110. loff_t isize = mapping->host->i_size;
  111. if (pos + len > isize)
  112. vmtruncate(mapping->host, isize);
  113. }
  114. return ret;
  115. }
  116. static sector_t udf_bmap(struct address_space *mapping, sector_t block)
  117. {
  118. return generic_block_bmap(mapping, block, udf_get_block);
  119. }
  120. const struct address_space_operations udf_aops = {
  121. .readpage = udf_readpage,
  122. .writepage = udf_writepage,
  123. .sync_page = block_sync_page,
  124. .write_begin = udf_write_begin,
  125. .write_end = generic_write_end,
  126. .bmap = udf_bmap,
  127. };
  128. void udf_expand_file_adinicb(struct inode *inode, int newsize, int *err)
  129. {
  130. struct page *page;
  131. char *kaddr;
  132. struct udf_inode_info *iinfo = UDF_I(inode);
  133. struct writeback_control udf_wbc = {
  134. .sync_mode = WB_SYNC_NONE,
  135. .nr_to_write = 1,
  136. };
  137. /* from now on we have normal address_space methods */
  138. inode->i_data.a_ops = &udf_aops;
  139. if (!iinfo->i_lenAlloc) {
  140. if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
  141. iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
  142. else
  143. iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
  144. mark_inode_dirty(inode);
  145. return;
  146. }
  147. page = grab_cache_page(inode->i_mapping, 0);
  148. BUG_ON(!PageLocked(page));
  149. if (!PageUptodate(page)) {
  150. kaddr = kmap(page);
  151. memset(kaddr + iinfo->i_lenAlloc, 0x00,
  152. PAGE_CACHE_SIZE - iinfo->i_lenAlloc);
  153. memcpy(kaddr, iinfo->i_ext.i_data + iinfo->i_lenEAttr,
  154. iinfo->i_lenAlloc);
  155. flush_dcache_page(page);
  156. SetPageUptodate(page);
  157. kunmap(page);
  158. }
  159. memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0x00,
  160. iinfo->i_lenAlloc);
  161. iinfo->i_lenAlloc = 0;
  162. if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
  163. iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
  164. else
  165. iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
  166. inode->i_data.a_ops->writepage(page, &udf_wbc);
  167. page_cache_release(page);
  168. mark_inode_dirty(inode);
  169. }
  170. struct buffer_head *udf_expand_dir_adinicb(struct inode *inode, int *block,
  171. int *err)
  172. {
  173. int newblock;
  174. struct buffer_head *dbh = NULL;
  175. struct kernel_lb_addr eloc;
  176. uint8_t alloctype;
  177. struct extent_position epos;
  178. struct udf_fileident_bh sfibh, dfibh;
  179. loff_t f_pos = udf_ext0_offset(inode);
  180. int size = udf_ext0_offset(inode) + inode->i_size;
  181. struct fileIdentDesc cfi, *sfi, *dfi;
  182. struct udf_inode_info *iinfo = UDF_I(inode);
  183. if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
  184. alloctype = ICBTAG_FLAG_AD_SHORT;
  185. else
  186. alloctype = ICBTAG_FLAG_AD_LONG;
  187. if (!inode->i_size) {
  188. iinfo->i_alloc_type = alloctype;
  189. mark_inode_dirty(inode);
  190. return NULL;
  191. }
  192. /* alloc block, and copy data to it */
  193. *block = udf_new_block(inode->i_sb, inode,
  194. iinfo->i_location.partitionReferenceNum,
  195. iinfo->i_location.logicalBlockNum, err);
  196. if (!(*block))
  197. return NULL;
  198. newblock = udf_get_pblock(inode->i_sb, *block,
  199. iinfo->i_location.partitionReferenceNum,
  200. 0);
  201. if (!newblock)
  202. return NULL;
  203. dbh = udf_tgetblk(inode->i_sb, newblock);
  204. if (!dbh)
  205. return NULL;
  206. lock_buffer(dbh);
  207. memset(dbh->b_data, 0x00, inode->i_sb->s_blocksize);
  208. set_buffer_uptodate(dbh);
  209. unlock_buffer(dbh);
  210. mark_buffer_dirty_inode(dbh, inode);
  211. sfibh.soffset = sfibh.eoffset =
  212. f_pos & (inode->i_sb->s_blocksize - 1);
  213. sfibh.sbh = sfibh.ebh = NULL;
  214. dfibh.soffset = dfibh.eoffset = 0;
  215. dfibh.sbh = dfibh.ebh = dbh;
  216. while (f_pos < size) {
  217. iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
  218. sfi = udf_fileident_read(inode, &f_pos, &sfibh, &cfi, NULL,
  219. NULL, NULL, NULL);
  220. if (!sfi) {
  221. brelse(dbh);
  222. return NULL;
  223. }
  224. iinfo->i_alloc_type = alloctype;
  225. sfi->descTag.tagLocation = cpu_to_le32(*block);
  226. dfibh.soffset = dfibh.eoffset;
  227. dfibh.eoffset += (sfibh.eoffset - sfibh.soffset);
  228. dfi = (struct fileIdentDesc *)(dbh->b_data + dfibh.soffset);
  229. if (udf_write_fi(inode, sfi, dfi, &dfibh, sfi->impUse,
  230. sfi->fileIdent +
  231. le16_to_cpu(sfi->lengthOfImpUse))) {
  232. iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
  233. brelse(dbh);
  234. return NULL;
  235. }
  236. }
  237. mark_buffer_dirty_inode(dbh, inode);
  238. memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0,
  239. iinfo->i_lenAlloc);
  240. iinfo->i_lenAlloc = 0;
  241. eloc.logicalBlockNum = *block;
  242. eloc.partitionReferenceNum =
  243. iinfo->i_location.partitionReferenceNum;
  244. iinfo->i_lenExtents = inode->i_size;
  245. epos.bh = NULL;
  246. epos.block = iinfo->i_location;
  247. epos.offset = udf_file_entry_alloc_offset(inode);
  248. udf_add_aext(inode, &epos, &eloc, inode->i_size, 0);
  249. /* UniqueID stuff */
  250. brelse(epos.bh);
  251. mark_inode_dirty(inode);
  252. return dbh;
  253. }
  254. static int udf_get_block(struct inode *inode, sector_t block,
  255. struct buffer_head *bh_result, int create)
  256. {
  257. int err, new;
  258. struct buffer_head *bh;
  259. sector_t phys = 0;
  260. struct udf_inode_info *iinfo;
  261. if (!create) {
  262. phys = udf_block_map(inode, block);
  263. if (phys)
  264. map_bh(bh_result, inode->i_sb, phys);
  265. return 0;
  266. }
  267. err = -EIO;
  268. new = 0;
  269. bh = NULL;
  270. lock_kernel();
  271. iinfo = UDF_I(inode);
  272. if (block == iinfo->i_next_alloc_block + 1) {
  273. iinfo->i_next_alloc_block++;
  274. iinfo->i_next_alloc_goal++;
  275. }
  276. err = 0;
  277. bh = inode_getblk(inode, block, &err, &phys, &new);
  278. BUG_ON(bh);
  279. if (err)
  280. goto abort;
  281. BUG_ON(!phys);
  282. if (new)
  283. set_buffer_new(bh_result);
  284. map_bh(bh_result, inode->i_sb, phys);
  285. abort:
  286. unlock_kernel();
  287. return err;
  288. }
  289. static struct buffer_head *udf_getblk(struct inode *inode, long block,
  290. int create, int *err)
  291. {
  292. struct buffer_head *bh;
  293. struct buffer_head dummy;
  294. dummy.b_state = 0;
  295. dummy.b_blocknr = -1000;
  296. *err = udf_get_block(inode, block, &dummy, create);
  297. if (!*err && buffer_mapped(&dummy)) {
  298. bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
  299. if (buffer_new(&dummy)) {
  300. lock_buffer(bh);
  301. memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
  302. set_buffer_uptodate(bh);
  303. unlock_buffer(bh);
  304. mark_buffer_dirty_inode(bh, inode);
  305. }
  306. return bh;
  307. }
  308. return NULL;
  309. }
  310. /* Extend the file by 'blocks' blocks, return the number of extents added */
  311. int udf_extend_file(struct inode *inode, struct extent_position *last_pos,
  312. struct kernel_long_ad *last_ext, sector_t blocks)
  313. {
  314. sector_t add;
  315. int count = 0, fake = !(last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
  316. struct super_block *sb = inode->i_sb;
  317. struct kernel_lb_addr prealloc_loc = {};
  318. int prealloc_len = 0;
  319. struct udf_inode_info *iinfo;
  320. /* The previous extent is fake and we should not extend by anything
  321. * - there's nothing to do... */
  322. if (!blocks && fake)
  323. return 0;
  324. iinfo = UDF_I(inode);
  325. /* Round the last extent up to a multiple of block size */
  326. if (last_ext->extLength & (sb->s_blocksize - 1)) {
  327. last_ext->extLength =
  328. (last_ext->extLength & UDF_EXTENT_FLAG_MASK) |
  329. (((last_ext->extLength & UDF_EXTENT_LENGTH_MASK) +
  330. sb->s_blocksize - 1) & ~(sb->s_blocksize - 1));
  331. iinfo->i_lenExtents =
  332. (iinfo->i_lenExtents + sb->s_blocksize - 1) &
  333. ~(sb->s_blocksize - 1);
  334. }
  335. /* Last extent are just preallocated blocks? */
  336. if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
  337. EXT_NOT_RECORDED_ALLOCATED) {
  338. /* Save the extent so that we can reattach it to the end */
  339. prealloc_loc = last_ext->extLocation;
  340. prealloc_len = last_ext->extLength;
  341. /* Mark the extent as a hole */
  342. last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
  343. (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
  344. last_ext->extLocation.logicalBlockNum = 0;
  345. last_ext->extLocation.partitionReferenceNum = 0;
  346. }
  347. /* Can we merge with the previous extent? */
  348. if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
  349. EXT_NOT_RECORDED_NOT_ALLOCATED) {
  350. add = ((1 << 30) - sb->s_blocksize -
  351. (last_ext->extLength & UDF_EXTENT_LENGTH_MASK)) >>
  352. sb->s_blocksize_bits;
  353. if (add > blocks)
  354. add = blocks;
  355. blocks -= add;
  356. last_ext->extLength += add << sb->s_blocksize_bits;
  357. }
  358. if (fake) {
  359. udf_add_aext(inode, last_pos, &last_ext->extLocation,
  360. last_ext->extLength, 1);
  361. count++;
  362. } else
  363. udf_write_aext(inode, last_pos, &last_ext->extLocation,
  364. last_ext->extLength, 1);
  365. /* Managed to do everything necessary? */
  366. if (!blocks)
  367. goto out;
  368. /* All further extents will be NOT_RECORDED_NOT_ALLOCATED */
  369. last_ext->extLocation.logicalBlockNum = 0;
  370. last_ext->extLocation.partitionReferenceNum = 0;
  371. add = (1 << (30-sb->s_blocksize_bits)) - 1;
  372. last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
  373. (add << sb->s_blocksize_bits);
  374. /* Create enough extents to cover the whole hole */
  375. while (blocks > add) {
  376. blocks -= add;
  377. if (udf_add_aext(inode, last_pos, &last_ext->extLocation,
  378. last_ext->extLength, 1) == -1)
  379. return -1;
  380. count++;
  381. }
  382. if (blocks) {
  383. last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
  384. (blocks << sb->s_blocksize_bits);
  385. if (udf_add_aext(inode, last_pos, &last_ext->extLocation,
  386. last_ext->extLength, 1) == -1)
  387. return -1;
  388. count++;
  389. }
  390. out:
  391. /* Do we have some preallocated blocks saved? */
  392. if (prealloc_len) {
  393. if (udf_add_aext(inode, last_pos, &prealloc_loc,
  394. prealloc_len, 1) == -1)
  395. return -1;
  396. last_ext->extLocation = prealloc_loc;
  397. last_ext->extLength = prealloc_len;
  398. count++;
  399. }
  400. /* last_pos should point to the last written extent... */
  401. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
  402. last_pos->offset -= sizeof(struct short_ad);
  403. else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
  404. last_pos->offset -= sizeof(struct long_ad);
  405. else
  406. return -1;
  407. return count;
  408. }
  409. static struct buffer_head *inode_getblk(struct inode *inode, sector_t block,
  410. int *err, sector_t *phys, int *new)
  411. {
  412. static sector_t last_block;
  413. struct buffer_head *result = NULL;
  414. struct kernel_long_ad laarr[EXTENT_MERGE_SIZE];
  415. struct extent_position prev_epos, cur_epos, next_epos;
  416. int count = 0, startnum = 0, endnum = 0;
  417. uint32_t elen = 0, tmpelen;
  418. struct kernel_lb_addr eloc, tmpeloc;
  419. int c = 1;
  420. loff_t lbcount = 0, b_off = 0;
  421. uint32_t newblocknum, newblock;
  422. sector_t offset = 0;
  423. int8_t etype;
  424. struct udf_inode_info *iinfo = UDF_I(inode);
  425. int goal = 0, pgoal = iinfo->i_location.logicalBlockNum;
  426. int lastblock = 0;
  427. prev_epos.offset = udf_file_entry_alloc_offset(inode);
  428. prev_epos.block = iinfo->i_location;
  429. prev_epos.bh = NULL;
  430. cur_epos = next_epos = prev_epos;
  431. b_off = (loff_t)block << inode->i_sb->s_blocksize_bits;
  432. /* find the extent which contains the block we are looking for.
  433. alternate between laarr[0] and laarr[1] for locations of the
  434. current extent, and the previous extent */
  435. do {
  436. if (prev_epos.bh != cur_epos.bh) {
  437. brelse(prev_epos.bh);
  438. get_bh(cur_epos.bh);
  439. prev_epos.bh = cur_epos.bh;
  440. }
  441. if (cur_epos.bh != next_epos.bh) {
  442. brelse(cur_epos.bh);
  443. get_bh(next_epos.bh);
  444. cur_epos.bh = next_epos.bh;
  445. }
  446. lbcount += elen;
  447. prev_epos.block = cur_epos.block;
  448. cur_epos.block = next_epos.block;
  449. prev_epos.offset = cur_epos.offset;
  450. cur_epos.offset = next_epos.offset;
  451. etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 1);
  452. if (etype == -1)
  453. break;
  454. c = !c;
  455. laarr[c].extLength = (etype << 30) | elen;
  456. laarr[c].extLocation = eloc;
  457. if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
  458. pgoal = eloc.logicalBlockNum +
  459. ((elen + inode->i_sb->s_blocksize - 1) >>
  460. inode->i_sb->s_blocksize_bits);
  461. count++;
  462. } while (lbcount + elen <= b_off);
  463. b_off -= lbcount;
  464. offset = b_off >> inode->i_sb->s_blocksize_bits;
  465. /*
  466. * Move prev_epos and cur_epos into indirect extent if we are at
  467. * the pointer to it
  468. */
  469. udf_next_aext(inode, &prev_epos, &tmpeloc, &tmpelen, 0);
  470. udf_next_aext(inode, &cur_epos, &tmpeloc, &tmpelen, 0);
  471. /* if the extent is allocated and recorded, return the block
  472. if the extent is not a multiple of the blocksize, round up */
  473. if (etype == (EXT_RECORDED_ALLOCATED >> 30)) {
  474. if (elen & (inode->i_sb->s_blocksize - 1)) {
  475. elen = EXT_RECORDED_ALLOCATED |
  476. ((elen + inode->i_sb->s_blocksize - 1) &
  477. ~(inode->i_sb->s_blocksize - 1));
  478. etype = udf_write_aext(inode, &cur_epos, &eloc, elen, 1);
  479. }
  480. brelse(prev_epos.bh);
  481. brelse(cur_epos.bh);
  482. brelse(next_epos.bh);
  483. newblock = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
  484. *phys = newblock;
  485. return NULL;
  486. }
  487. last_block = block;
  488. /* Are we beyond EOF? */
  489. if (etype == -1) {
  490. int ret;
  491. if (count) {
  492. if (c)
  493. laarr[0] = laarr[1];
  494. startnum = 1;
  495. } else {
  496. /* Create a fake extent when there's not one */
  497. memset(&laarr[0].extLocation, 0x00,
  498. sizeof(struct kernel_lb_addr));
  499. laarr[0].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
  500. /* Will udf_extend_file() create real extent from
  501. a fake one? */
  502. startnum = (offset > 0);
  503. }
  504. /* Create extents for the hole between EOF and offset */
  505. ret = udf_extend_file(inode, &prev_epos, laarr, offset);
  506. if (ret == -1) {
  507. brelse(prev_epos.bh);
  508. brelse(cur_epos.bh);
  509. brelse(next_epos.bh);
  510. /* We don't really know the error here so we just make
  511. * something up */
  512. *err = -ENOSPC;
  513. return NULL;
  514. }
  515. c = 0;
  516. offset = 0;
  517. count += ret;
  518. /* We are not covered by a preallocated extent? */
  519. if ((laarr[0].extLength & UDF_EXTENT_FLAG_MASK) !=
  520. EXT_NOT_RECORDED_ALLOCATED) {
  521. /* Is there any real extent? - otherwise we overwrite
  522. * the fake one... */
  523. if (count)
  524. c = !c;
  525. laarr[c].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
  526. inode->i_sb->s_blocksize;
  527. memset(&laarr[c].extLocation, 0x00,
  528. sizeof(struct kernel_lb_addr));
  529. count++;
  530. endnum++;
  531. }
  532. endnum = c + 1;
  533. lastblock = 1;
  534. } else {
  535. endnum = startnum = ((count > 2) ? 2 : count);
  536. /* if the current extent is in position 0,
  537. swap it with the previous */
  538. if (!c && count != 1) {
  539. laarr[2] = laarr[0];
  540. laarr[0] = laarr[1];
  541. laarr[1] = laarr[2];
  542. c = 1;
  543. }
  544. /* if the current block is located in an extent,
  545. read the next extent */
  546. etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 0);
  547. if (etype != -1) {
  548. laarr[c + 1].extLength = (etype << 30) | elen;
  549. laarr[c + 1].extLocation = eloc;
  550. count++;
  551. startnum++;
  552. endnum++;
  553. } else
  554. lastblock = 1;
  555. }
  556. /* if the current extent is not recorded but allocated, get the
  557. * block in the extent corresponding to the requested block */
  558. if ((laarr[c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30))
  559. newblocknum = laarr[c].extLocation.logicalBlockNum + offset;
  560. else { /* otherwise, allocate a new block */
  561. if (iinfo->i_next_alloc_block == block)
  562. goal = iinfo->i_next_alloc_goal;
  563. if (!goal) {
  564. if (!(goal = pgoal)) /* XXX: what was intended here? */
  565. goal = iinfo->i_location.logicalBlockNum + 1;
  566. }
  567. newblocknum = udf_new_block(inode->i_sb, inode,
  568. iinfo->i_location.partitionReferenceNum,
  569. goal, err);
  570. if (!newblocknum) {
  571. brelse(prev_epos.bh);
  572. *err = -ENOSPC;
  573. return NULL;
  574. }
  575. iinfo->i_lenExtents += inode->i_sb->s_blocksize;
  576. }
  577. /* if the extent the requsted block is located in contains multiple
  578. * blocks, split the extent into at most three extents. blocks prior
  579. * to requested block, requested block, and blocks after requested
  580. * block */
  581. udf_split_extents(inode, &c, offset, newblocknum, laarr, &endnum);
  582. #ifdef UDF_PREALLOCATE
  583. /* We preallocate blocks only for regular files. It also makes sense
  584. * for directories but there's a problem when to drop the
  585. * preallocation. We might use some delayed work for that but I feel
  586. * it's overengineering for a filesystem like UDF. */
  587. if (S_ISREG(inode->i_mode))
  588. udf_prealloc_extents(inode, c, lastblock, laarr, &endnum);
  589. #endif
  590. /* merge any continuous blocks in laarr */
  591. udf_merge_extents(inode, laarr, &endnum);
  592. /* write back the new extents, inserting new extents if the new number
  593. * of extents is greater than the old number, and deleting extents if
  594. * the new number of extents is less than the old number */
  595. udf_update_extents(inode, laarr, startnum, endnum, &prev_epos);
  596. brelse(prev_epos.bh);
  597. newblock = udf_get_pblock(inode->i_sb, newblocknum,
  598. iinfo->i_location.partitionReferenceNum, 0);
  599. if (!newblock)
  600. return NULL;
  601. *phys = newblock;
  602. *err = 0;
  603. *new = 1;
  604. iinfo->i_next_alloc_block = block;
  605. iinfo->i_next_alloc_goal = newblocknum;
  606. inode->i_ctime = current_fs_time(inode->i_sb);
  607. if (IS_SYNC(inode))
  608. udf_sync_inode(inode);
  609. else
  610. mark_inode_dirty(inode);
  611. return result;
  612. }
  613. static void udf_split_extents(struct inode *inode, int *c, int offset,
  614. int newblocknum,
  615. struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
  616. int *endnum)
  617. {
  618. unsigned long blocksize = inode->i_sb->s_blocksize;
  619. unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
  620. if ((laarr[*c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30) ||
  621. (laarr[*c].extLength >> 30) ==
  622. (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
  623. int curr = *c;
  624. int blen = ((laarr[curr].extLength & UDF_EXTENT_LENGTH_MASK) +
  625. blocksize - 1) >> blocksize_bits;
  626. int8_t etype = (laarr[curr].extLength >> 30);
  627. if (blen == 1)
  628. ;
  629. else if (!offset || blen == offset + 1) {
  630. laarr[curr + 2] = laarr[curr + 1];
  631. laarr[curr + 1] = laarr[curr];
  632. } else {
  633. laarr[curr + 3] = laarr[curr + 1];
  634. laarr[curr + 2] = laarr[curr + 1] = laarr[curr];
  635. }
  636. if (offset) {
  637. if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
  638. udf_free_blocks(inode->i_sb, inode,
  639. &laarr[curr].extLocation,
  640. 0, offset);
  641. laarr[curr].extLength =
  642. EXT_NOT_RECORDED_NOT_ALLOCATED |
  643. (offset << blocksize_bits);
  644. laarr[curr].extLocation.logicalBlockNum = 0;
  645. laarr[curr].extLocation.
  646. partitionReferenceNum = 0;
  647. } else
  648. laarr[curr].extLength = (etype << 30) |
  649. (offset << blocksize_bits);
  650. curr++;
  651. (*c)++;
  652. (*endnum)++;
  653. }
  654. laarr[curr].extLocation.logicalBlockNum = newblocknum;
  655. if (etype == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
  656. laarr[curr].extLocation.partitionReferenceNum =
  657. UDF_I(inode)->i_location.partitionReferenceNum;
  658. laarr[curr].extLength = EXT_RECORDED_ALLOCATED |
  659. blocksize;
  660. curr++;
  661. if (blen != offset + 1) {
  662. if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30))
  663. laarr[curr].extLocation.logicalBlockNum +=
  664. offset + 1;
  665. laarr[curr].extLength = (etype << 30) |
  666. ((blen - (offset + 1)) << blocksize_bits);
  667. curr++;
  668. (*endnum)++;
  669. }
  670. }
  671. }
  672. static void udf_prealloc_extents(struct inode *inode, int c, int lastblock,
  673. struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
  674. int *endnum)
  675. {
  676. int start, length = 0, currlength = 0, i;
  677. if (*endnum >= (c + 1)) {
  678. if (!lastblock)
  679. return;
  680. else
  681. start = c;
  682. } else {
  683. if ((laarr[c + 1].extLength >> 30) ==
  684. (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
  685. start = c + 1;
  686. length = currlength =
  687. (((laarr[c + 1].extLength &
  688. UDF_EXTENT_LENGTH_MASK) +
  689. inode->i_sb->s_blocksize - 1) >>
  690. inode->i_sb->s_blocksize_bits);
  691. } else
  692. start = c;
  693. }
  694. for (i = start + 1; i <= *endnum; i++) {
  695. if (i == *endnum) {
  696. if (lastblock)
  697. length += UDF_DEFAULT_PREALLOC_BLOCKS;
  698. } else if ((laarr[i].extLength >> 30) ==
  699. (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
  700. length += (((laarr[i].extLength &
  701. UDF_EXTENT_LENGTH_MASK) +
  702. inode->i_sb->s_blocksize - 1) >>
  703. inode->i_sb->s_blocksize_bits);
  704. } else
  705. break;
  706. }
  707. if (length) {
  708. int next = laarr[start].extLocation.logicalBlockNum +
  709. (((laarr[start].extLength & UDF_EXTENT_LENGTH_MASK) +
  710. inode->i_sb->s_blocksize - 1) >>
  711. inode->i_sb->s_blocksize_bits);
  712. int numalloc = udf_prealloc_blocks(inode->i_sb, inode,
  713. laarr[start].extLocation.partitionReferenceNum,
  714. next, (UDF_DEFAULT_PREALLOC_BLOCKS > length ?
  715. length : UDF_DEFAULT_PREALLOC_BLOCKS) -
  716. currlength);
  717. if (numalloc) {
  718. if (start == (c + 1))
  719. laarr[start].extLength +=
  720. (numalloc <<
  721. inode->i_sb->s_blocksize_bits);
  722. else {
  723. memmove(&laarr[c + 2], &laarr[c + 1],
  724. sizeof(struct long_ad) * (*endnum - (c + 1)));
  725. (*endnum)++;
  726. laarr[c + 1].extLocation.logicalBlockNum = next;
  727. laarr[c + 1].extLocation.partitionReferenceNum =
  728. laarr[c].extLocation.
  729. partitionReferenceNum;
  730. laarr[c + 1].extLength =
  731. EXT_NOT_RECORDED_ALLOCATED |
  732. (numalloc <<
  733. inode->i_sb->s_blocksize_bits);
  734. start = c + 1;
  735. }
  736. for (i = start + 1; numalloc && i < *endnum; i++) {
  737. int elen = ((laarr[i].extLength &
  738. UDF_EXTENT_LENGTH_MASK) +
  739. inode->i_sb->s_blocksize - 1) >>
  740. inode->i_sb->s_blocksize_bits;
  741. if (elen > numalloc) {
  742. laarr[i].extLength -=
  743. (numalloc <<
  744. inode->i_sb->s_blocksize_bits);
  745. numalloc = 0;
  746. } else {
  747. numalloc -= elen;
  748. if (*endnum > (i + 1))
  749. memmove(&laarr[i],
  750. &laarr[i + 1],
  751. sizeof(struct long_ad) *
  752. (*endnum - (i + 1)));
  753. i--;
  754. (*endnum)--;
  755. }
  756. }
  757. UDF_I(inode)->i_lenExtents +=
  758. numalloc << inode->i_sb->s_blocksize_bits;
  759. }
  760. }
  761. }
  762. static void udf_merge_extents(struct inode *inode,
  763. struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
  764. int *endnum)
  765. {
  766. int i;
  767. unsigned long blocksize = inode->i_sb->s_blocksize;
  768. unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
  769. for (i = 0; i < (*endnum - 1); i++) {
  770. struct kernel_long_ad *li /*l[i]*/ = &laarr[i];
  771. struct kernel_long_ad *lip1 /*l[i plus 1]*/ = &laarr[i + 1];
  772. if (((li->extLength >> 30) == (lip1->extLength >> 30)) &&
  773. (((li->extLength >> 30) ==
  774. (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) ||
  775. ((lip1->extLocation.logicalBlockNum -
  776. li->extLocation.logicalBlockNum) ==
  777. (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
  778. blocksize - 1) >> blocksize_bits)))) {
  779. if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
  780. (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
  781. blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
  782. lip1->extLength = (lip1->extLength -
  783. (li->extLength &
  784. UDF_EXTENT_LENGTH_MASK) +
  785. UDF_EXTENT_LENGTH_MASK) &
  786. ~(blocksize - 1);
  787. li->extLength = (li->extLength &
  788. UDF_EXTENT_FLAG_MASK) +
  789. (UDF_EXTENT_LENGTH_MASK + 1) -
  790. blocksize;
  791. lip1->extLocation.logicalBlockNum =
  792. li->extLocation.logicalBlockNum +
  793. ((li->extLength &
  794. UDF_EXTENT_LENGTH_MASK) >>
  795. blocksize_bits);
  796. } else {
  797. li->extLength = lip1->extLength +
  798. (((li->extLength &
  799. UDF_EXTENT_LENGTH_MASK) +
  800. blocksize - 1) & ~(blocksize - 1));
  801. if (*endnum > (i + 2))
  802. memmove(&laarr[i + 1], &laarr[i + 2],
  803. sizeof(struct long_ad) *
  804. (*endnum - (i + 2)));
  805. i--;
  806. (*endnum)--;
  807. }
  808. } else if (((li->extLength >> 30) ==
  809. (EXT_NOT_RECORDED_ALLOCATED >> 30)) &&
  810. ((lip1->extLength >> 30) ==
  811. (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))) {
  812. udf_free_blocks(inode->i_sb, inode, &li->extLocation, 0,
  813. ((li->extLength &
  814. UDF_EXTENT_LENGTH_MASK) +
  815. blocksize - 1) >> blocksize_bits);
  816. li->extLocation.logicalBlockNum = 0;
  817. li->extLocation.partitionReferenceNum = 0;
  818. if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
  819. (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
  820. blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
  821. lip1->extLength = (lip1->extLength -
  822. (li->extLength &
  823. UDF_EXTENT_LENGTH_MASK) +
  824. UDF_EXTENT_LENGTH_MASK) &
  825. ~(blocksize - 1);
  826. li->extLength = (li->extLength &
  827. UDF_EXTENT_FLAG_MASK) +
  828. (UDF_EXTENT_LENGTH_MASK + 1) -
  829. blocksize;
  830. } else {
  831. li->extLength = lip1->extLength +
  832. (((li->extLength &
  833. UDF_EXTENT_LENGTH_MASK) +
  834. blocksize - 1) & ~(blocksize - 1));
  835. if (*endnum > (i + 2))
  836. memmove(&laarr[i + 1], &laarr[i + 2],
  837. sizeof(struct long_ad) *
  838. (*endnum - (i + 2)));
  839. i--;
  840. (*endnum)--;
  841. }
  842. } else if ((li->extLength >> 30) ==
  843. (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
  844. udf_free_blocks(inode->i_sb, inode,
  845. &li->extLocation, 0,
  846. ((li->extLength &
  847. UDF_EXTENT_LENGTH_MASK) +
  848. blocksize - 1) >> blocksize_bits);
  849. li->extLocation.logicalBlockNum = 0;
  850. li->extLocation.partitionReferenceNum = 0;
  851. li->extLength = (li->extLength &
  852. UDF_EXTENT_LENGTH_MASK) |
  853. EXT_NOT_RECORDED_NOT_ALLOCATED;
  854. }
  855. }
  856. }
  857. static void udf_update_extents(struct inode *inode,
  858. struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
  859. int startnum, int endnum,
  860. struct extent_position *epos)
  861. {
  862. int start = 0, i;
  863. struct kernel_lb_addr tmploc;
  864. uint32_t tmplen;
  865. if (startnum > endnum) {
  866. for (i = 0; i < (startnum - endnum); i++)
  867. udf_delete_aext(inode, *epos, laarr[i].extLocation,
  868. laarr[i].extLength);
  869. } else if (startnum < endnum) {
  870. for (i = 0; i < (endnum - startnum); i++) {
  871. udf_insert_aext(inode, *epos, laarr[i].extLocation,
  872. laarr[i].extLength);
  873. udf_next_aext(inode, epos, &laarr[i].extLocation,
  874. &laarr[i].extLength, 1);
  875. start++;
  876. }
  877. }
  878. for (i = start; i < endnum; i++) {
  879. udf_next_aext(inode, epos, &tmploc, &tmplen, 0);
  880. udf_write_aext(inode, epos, &laarr[i].extLocation,
  881. laarr[i].extLength, 1);
  882. }
  883. }
  884. struct buffer_head *udf_bread(struct inode *inode, int block,
  885. int create, int *err)
  886. {
  887. struct buffer_head *bh = NULL;
  888. bh = udf_getblk(inode, block, create, err);
  889. if (!bh)
  890. return NULL;
  891. if (buffer_uptodate(bh))
  892. return bh;
  893. ll_rw_block(READ, 1, &bh);
  894. wait_on_buffer(bh);
  895. if (buffer_uptodate(bh))
  896. return bh;
  897. brelse(bh);
  898. *err = -EIO;
  899. return NULL;
  900. }
  901. void udf_truncate(struct inode *inode)
  902. {
  903. int offset;
  904. int err;
  905. struct udf_inode_info *iinfo;
  906. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  907. S_ISLNK(inode->i_mode)))
  908. return;
  909. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  910. return;
  911. lock_kernel();
  912. iinfo = UDF_I(inode);
  913. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
  914. if (inode->i_sb->s_blocksize <
  915. (udf_file_entry_alloc_offset(inode) +
  916. inode->i_size)) {
  917. udf_expand_file_adinicb(inode, inode->i_size, &err);
  918. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
  919. inode->i_size = iinfo->i_lenAlloc;
  920. unlock_kernel();
  921. return;
  922. } else
  923. udf_truncate_extents(inode);
  924. } else {
  925. offset = inode->i_size & (inode->i_sb->s_blocksize - 1);
  926. memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr + offset,
  927. 0x00, inode->i_sb->s_blocksize -
  928. offset - udf_file_entry_alloc_offset(inode));
  929. iinfo->i_lenAlloc = inode->i_size;
  930. }
  931. } else {
  932. block_truncate_page(inode->i_mapping, inode->i_size,
  933. udf_get_block);
  934. udf_truncate_extents(inode);
  935. }
  936. inode->i_mtime = inode->i_ctime = current_fs_time(inode->i_sb);
  937. if (IS_SYNC(inode))
  938. udf_sync_inode(inode);
  939. else
  940. mark_inode_dirty(inode);
  941. unlock_kernel();
  942. }
  943. static void __udf_read_inode(struct inode *inode)
  944. {
  945. struct buffer_head *bh = NULL;
  946. struct fileEntry *fe;
  947. uint16_t ident;
  948. struct udf_inode_info *iinfo = UDF_I(inode);
  949. /*
  950. * Set defaults, but the inode is still incomplete!
  951. * Note: get_new_inode() sets the following on a new inode:
  952. * i_sb = sb
  953. * i_no = ino
  954. * i_flags = sb->s_flags
  955. * i_state = 0
  956. * clean_inode(): zero fills and sets
  957. * i_count = 1
  958. * i_nlink = 1
  959. * i_op = NULL;
  960. */
  961. bh = udf_read_ptagged(inode->i_sb, &iinfo->i_location, 0, &ident);
  962. if (!bh) {
  963. printk(KERN_ERR "udf: udf_read_inode(ino %ld) failed !bh\n",
  964. inode->i_ino);
  965. make_bad_inode(inode);
  966. return;
  967. }
  968. if (ident != TAG_IDENT_FE && ident != TAG_IDENT_EFE &&
  969. ident != TAG_IDENT_USE) {
  970. printk(KERN_ERR "udf: udf_read_inode(ino %ld) "
  971. "failed ident=%d\n", inode->i_ino, ident);
  972. brelse(bh);
  973. make_bad_inode(inode);
  974. return;
  975. }
  976. fe = (struct fileEntry *)bh->b_data;
  977. if (fe->icbTag.strategyType == cpu_to_le16(4096)) {
  978. struct buffer_head *ibh;
  979. ibh = udf_read_ptagged(inode->i_sb, &iinfo->i_location, 1,
  980. &ident);
  981. if (ident == TAG_IDENT_IE && ibh) {
  982. struct buffer_head *nbh = NULL;
  983. struct kernel_lb_addr loc;
  984. struct indirectEntry *ie;
  985. ie = (struct indirectEntry *)ibh->b_data;
  986. loc = lelb_to_cpu(ie->indirectICB.extLocation);
  987. if (ie->indirectICB.extLength &&
  988. (nbh = udf_read_ptagged(inode->i_sb, &loc, 0,
  989. &ident))) {
  990. if (ident == TAG_IDENT_FE ||
  991. ident == TAG_IDENT_EFE) {
  992. memcpy(&iinfo->i_location,
  993. &loc,
  994. sizeof(struct kernel_lb_addr));
  995. brelse(bh);
  996. brelse(ibh);
  997. brelse(nbh);
  998. __udf_read_inode(inode);
  999. return;
  1000. }
  1001. brelse(nbh);
  1002. }
  1003. }
  1004. brelse(ibh);
  1005. } else if (fe->icbTag.strategyType != cpu_to_le16(4)) {
  1006. printk(KERN_ERR "udf: unsupported strategy type: %d\n",
  1007. le16_to_cpu(fe->icbTag.strategyType));
  1008. brelse(bh);
  1009. make_bad_inode(inode);
  1010. return;
  1011. }
  1012. udf_fill_inode(inode, bh);
  1013. brelse(bh);
  1014. }
  1015. static void udf_fill_inode(struct inode *inode, struct buffer_head *bh)
  1016. {
  1017. struct fileEntry *fe;
  1018. struct extendedFileEntry *efe;
  1019. int offset;
  1020. struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
  1021. struct udf_inode_info *iinfo = UDF_I(inode);
  1022. fe = (struct fileEntry *)bh->b_data;
  1023. efe = (struct extendedFileEntry *)bh->b_data;
  1024. if (fe->icbTag.strategyType == cpu_to_le16(4))
  1025. iinfo->i_strat4096 = 0;
  1026. else /* if (fe->icbTag.strategyType == cpu_to_le16(4096)) */
  1027. iinfo->i_strat4096 = 1;
  1028. iinfo->i_alloc_type = le16_to_cpu(fe->icbTag.flags) &
  1029. ICBTAG_FLAG_AD_MASK;
  1030. iinfo->i_unique = 0;
  1031. iinfo->i_lenEAttr = 0;
  1032. iinfo->i_lenExtents = 0;
  1033. iinfo->i_lenAlloc = 0;
  1034. iinfo->i_next_alloc_block = 0;
  1035. iinfo->i_next_alloc_goal = 0;
  1036. if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_EFE)) {
  1037. iinfo->i_efe = 1;
  1038. iinfo->i_use = 0;
  1039. if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
  1040. sizeof(struct extendedFileEntry))) {
  1041. make_bad_inode(inode);
  1042. return;
  1043. }
  1044. memcpy(iinfo->i_ext.i_data,
  1045. bh->b_data + sizeof(struct extendedFileEntry),
  1046. inode->i_sb->s_blocksize -
  1047. sizeof(struct extendedFileEntry));
  1048. } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_FE)) {
  1049. iinfo->i_efe = 0;
  1050. iinfo->i_use = 0;
  1051. if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
  1052. sizeof(struct fileEntry))) {
  1053. make_bad_inode(inode);
  1054. return;
  1055. }
  1056. memcpy(iinfo->i_ext.i_data,
  1057. bh->b_data + sizeof(struct fileEntry),
  1058. inode->i_sb->s_blocksize - sizeof(struct fileEntry));
  1059. } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_USE)) {
  1060. iinfo->i_efe = 0;
  1061. iinfo->i_use = 1;
  1062. iinfo->i_lenAlloc = le32_to_cpu(
  1063. ((struct unallocSpaceEntry *)bh->b_data)->
  1064. lengthAllocDescs);
  1065. if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
  1066. sizeof(struct unallocSpaceEntry))) {
  1067. make_bad_inode(inode);
  1068. return;
  1069. }
  1070. memcpy(iinfo->i_ext.i_data,
  1071. bh->b_data + sizeof(struct unallocSpaceEntry),
  1072. inode->i_sb->s_blocksize -
  1073. sizeof(struct unallocSpaceEntry));
  1074. return;
  1075. }
  1076. read_lock(&sbi->s_cred_lock);
  1077. inode->i_uid = le32_to_cpu(fe->uid);
  1078. if (inode->i_uid == -1 ||
  1079. UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_IGNORE) ||
  1080. UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_SET))
  1081. inode->i_uid = UDF_SB(inode->i_sb)->s_uid;
  1082. inode->i_gid = le32_to_cpu(fe->gid);
  1083. if (inode->i_gid == -1 ||
  1084. UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_IGNORE) ||
  1085. UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_SET))
  1086. inode->i_gid = UDF_SB(inode->i_sb)->s_gid;
  1087. if (fe->icbTag.fileType != ICBTAG_FILE_TYPE_DIRECTORY &&
  1088. sbi->s_fmode != UDF_INVALID_MODE)
  1089. inode->i_mode = sbi->s_fmode;
  1090. else if (fe->icbTag.fileType == ICBTAG_FILE_TYPE_DIRECTORY &&
  1091. sbi->s_dmode != UDF_INVALID_MODE)
  1092. inode->i_mode = sbi->s_dmode;
  1093. else
  1094. inode->i_mode = udf_convert_permissions(fe);
  1095. inode->i_mode &= ~sbi->s_umask;
  1096. read_unlock(&sbi->s_cred_lock);
  1097. inode->i_nlink = le16_to_cpu(fe->fileLinkCount);
  1098. if (!inode->i_nlink)
  1099. inode->i_nlink = 1;
  1100. inode->i_size = le64_to_cpu(fe->informationLength);
  1101. iinfo->i_lenExtents = inode->i_size;
  1102. if (iinfo->i_efe == 0) {
  1103. inode->i_blocks = le64_to_cpu(fe->logicalBlocksRecorded) <<
  1104. (inode->i_sb->s_blocksize_bits - 9);
  1105. if (!udf_disk_stamp_to_time(&inode->i_atime, fe->accessTime))
  1106. inode->i_atime = sbi->s_record_time;
  1107. if (!udf_disk_stamp_to_time(&inode->i_mtime,
  1108. fe->modificationTime))
  1109. inode->i_mtime = sbi->s_record_time;
  1110. if (!udf_disk_stamp_to_time(&inode->i_ctime, fe->attrTime))
  1111. inode->i_ctime = sbi->s_record_time;
  1112. iinfo->i_unique = le64_to_cpu(fe->uniqueID);
  1113. iinfo->i_lenEAttr = le32_to_cpu(fe->lengthExtendedAttr);
  1114. iinfo->i_lenAlloc = le32_to_cpu(fe->lengthAllocDescs);
  1115. offset = sizeof(struct fileEntry) + iinfo->i_lenEAttr;
  1116. } else {
  1117. inode->i_blocks = le64_to_cpu(efe->logicalBlocksRecorded) <<
  1118. (inode->i_sb->s_blocksize_bits - 9);
  1119. if (!udf_disk_stamp_to_time(&inode->i_atime, efe->accessTime))
  1120. inode->i_atime = sbi->s_record_time;
  1121. if (!udf_disk_stamp_to_time(&inode->i_mtime,
  1122. efe->modificationTime))
  1123. inode->i_mtime = sbi->s_record_time;
  1124. if (!udf_disk_stamp_to_time(&iinfo->i_crtime, efe->createTime))
  1125. iinfo->i_crtime = sbi->s_record_time;
  1126. if (!udf_disk_stamp_to_time(&inode->i_ctime, efe->attrTime))
  1127. inode->i_ctime = sbi->s_record_time;
  1128. iinfo->i_unique = le64_to_cpu(efe->uniqueID);
  1129. iinfo->i_lenEAttr = le32_to_cpu(efe->lengthExtendedAttr);
  1130. iinfo->i_lenAlloc = le32_to_cpu(efe->lengthAllocDescs);
  1131. offset = sizeof(struct extendedFileEntry) +
  1132. iinfo->i_lenEAttr;
  1133. }
  1134. switch (fe->icbTag.fileType) {
  1135. case ICBTAG_FILE_TYPE_DIRECTORY:
  1136. inode->i_op = &udf_dir_inode_operations;
  1137. inode->i_fop = &udf_dir_operations;
  1138. inode->i_mode |= S_IFDIR;
  1139. inc_nlink(inode);
  1140. break;
  1141. case ICBTAG_FILE_TYPE_REALTIME:
  1142. case ICBTAG_FILE_TYPE_REGULAR:
  1143. case ICBTAG_FILE_TYPE_UNDEF:
  1144. case ICBTAG_FILE_TYPE_VAT20:
  1145. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
  1146. inode->i_data.a_ops = &udf_adinicb_aops;
  1147. else
  1148. inode->i_data.a_ops = &udf_aops;
  1149. inode->i_op = &udf_file_inode_operations;
  1150. inode->i_fop = &udf_file_operations;
  1151. inode->i_mode |= S_IFREG;
  1152. break;
  1153. case ICBTAG_FILE_TYPE_BLOCK:
  1154. inode->i_mode |= S_IFBLK;
  1155. break;
  1156. case ICBTAG_FILE_TYPE_CHAR:
  1157. inode->i_mode |= S_IFCHR;
  1158. break;
  1159. case ICBTAG_FILE_TYPE_FIFO:
  1160. init_special_inode(inode, inode->i_mode | S_IFIFO, 0);
  1161. break;
  1162. case ICBTAG_FILE_TYPE_SOCKET:
  1163. init_special_inode(inode, inode->i_mode | S_IFSOCK, 0);
  1164. break;
  1165. case ICBTAG_FILE_TYPE_SYMLINK:
  1166. inode->i_data.a_ops = &udf_symlink_aops;
  1167. inode->i_op = &udf_symlink_inode_operations;
  1168. inode->i_mode = S_IFLNK | S_IRWXUGO;
  1169. break;
  1170. case ICBTAG_FILE_TYPE_MAIN:
  1171. udf_debug("METADATA FILE-----\n");
  1172. break;
  1173. case ICBTAG_FILE_TYPE_MIRROR:
  1174. udf_debug("METADATA MIRROR FILE-----\n");
  1175. break;
  1176. case ICBTAG_FILE_TYPE_BITMAP:
  1177. udf_debug("METADATA BITMAP FILE-----\n");
  1178. break;
  1179. default:
  1180. printk(KERN_ERR "udf: udf_fill_inode(ino %ld) failed unknown "
  1181. "file type=%d\n", inode->i_ino,
  1182. fe->icbTag.fileType);
  1183. make_bad_inode(inode);
  1184. return;
  1185. }
  1186. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  1187. struct deviceSpec *dsea =
  1188. (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
  1189. if (dsea) {
  1190. init_special_inode(inode, inode->i_mode,
  1191. MKDEV(le32_to_cpu(dsea->majorDeviceIdent),
  1192. le32_to_cpu(dsea->minorDeviceIdent)));
  1193. /* Developer ID ??? */
  1194. } else
  1195. make_bad_inode(inode);
  1196. }
  1197. }
  1198. static int udf_alloc_i_data(struct inode *inode, size_t size)
  1199. {
  1200. struct udf_inode_info *iinfo = UDF_I(inode);
  1201. iinfo->i_ext.i_data = kmalloc(size, GFP_KERNEL);
  1202. if (!iinfo->i_ext.i_data) {
  1203. printk(KERN_ERR "udf:udf_alloc_i_data (ino %ld) "
  1204. "no free memory\n", inode->i_ino);
  1205. return -ENOMEM;
  1206. }
  1207. return 0;
  1208. }
  1209. static mode_t udf_convert_permissions(struct fileEntry *fe)
  1210. {
  1211. mode_t mode;
  1212. uint32_t permissions;
  1213. uint32_t flags;
  1214. permissions = le32_to_cpu(fe->permissions);
  1215. flags = le16_to_cpu(fe->icbTag.flags);
  1216. mode = ((permissions) & S_IRWXO) |
  1217. ((permissions >> 2) & S_IRWXG) |
  1218. ((permissions >> 4) & S_IRWXU) |
  1219. ((flags & ICBTAG_FLAG_SETUID) ? S_ISUID : 0) |
  1220. ((flags & ICBTAG_FLAG_SETGID) ? S_ISGID : 0) |
  1221. ((flags & ICBTAG_FLAG_STICKY) ? S_ISVTX : 0);
  1222. return mode;
  1223. }
  1224. int udf_write_inode(struct inode *inode, struct writeback_control *wbc)
  1225. {
  1226. return udf_update_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
  1227. }
  1228. static int udf_sync_inode(struct inode *inode)
  1229. {
  1230. return udf_update_inode(inode, 1);
  1231. }
  1232. static int udf_update_inode(struct inode *inode, int do_sync)
  1233. {
  1234. struct buffer_head *bh = NULL;
  1235. struct fileEntry *fe;
  1236. struct extendedFileEntry *efe;
  1237. uint32_t udfperms;
  1238. uint16_t icbflags;
  1239. uint16_t crclen;
  1240. int err = 0;
  1241. struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
  1242. unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
  1243. struct udf_inode_info *iinfo = UDF_I(inode);
  1244. bh = udf_tgetblk(inode->i_sb,
  1245. udf_get_lb_pblock(inode->i_sb, &iinfo->i_location, 0));
  1246. if (!bh) {
  1247. udf_debug("getblk failure\n");
  1248. return -ENOMEM;
  1249. }
  1250. lock_buffer(bh);
  1251. memset(bh->b_data, 0, inode->i_sb->s_blocksize);
  1252. fe = (struct fileEntry *)bh->b_data;
  1253. efe = (struct extendedFileEntry *)bh->b_data;
  1254. if (iinfo->i_use) {
  1255. struct unallocSpaceEntry *use =
  1256. (struct unallocSpaceEntry *)bh->b_data;
  1257. use->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
  1258. memcpy(bh->b_data + sizeof(struct unallocSpaceEntry),
  1259. iinfo->i_ext.i_data, inode->i_sb->s_blocksize -
  1260. sizeof(struct unallocSpaceEntry));
  1261. use->descTag.tagIdent = cpu_to_le16(TAG_IDENT_USE);
  1262. use->descTag.tagLocation =
  1263. cpu_to_le32(iinfo->i_location.logicalBlockNum);
  1264. crclen = sizeof(struct unallocSpaceEntry) +
  1265. iinfo->i_lenAlloc - sizeof(struct tag);
  1266. use->descTag.descCRCLength = cpu_to_le16(crclen);
  1267. use->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)use +
  1268. sizeof(struct tag),
  1269. crclen));
  1270. use->descTag.tagChecksum = udf_tag_checksum(&use->descTag);
  1271. goto out;
  1272. }
  1273. if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_FORGET))
  1274. fe->uid = cpu_to_le32(-1);
  1275. else
  1276. fe->uid = cpu_to_le32(inode->i_uid);
  1277. if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_FORGET))
  1278. fe->gid = cpu_to_le32(-1);
  1279. else
  1280. fe->gid = cpu_to_le32(inode->i_gid);
  1281. udfperms = ((inode->i_mode & S_IRWXO)) |
  1282. ((inode->i_mode & S_IRWXG) << 2) |
  1283. ((inode->i_mode & S_IRWXU) << 4);
  1284. udfperms |= (le32_to_cpu(fe->permissions) &
  1285. (FE_PERM_O_DELETE | FE_PERM_O_CHATTR |
  1286. FE_PERM_G_DELETE | FE_PERM_G_CHATTR |
  1287. FE_PERM_U_DELETE | FE_PERM_U_CHATTR));
  1288. fe->permissions = cpu_to_le32(udfperms);
  1289. if (S_ISDIR(inode->i_mode))
  1290. fe->fileLinkCount = cpu_to_le16(inode->i_nlink - 1);
  1291. else
  1292. fe->fileLinkCount = cpu_to_le16(inode->i_nlink);
  1293. fe->informationLength = cpu_to_le64(inode->i_size);
  1294. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  1295. struct regid *eid;
  1296. struct deviceSpec *dsea =
  1297. (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
  1298. if (!dsea) {
  1299. dsea = (struct deviceSpec *)
  1300. udf_add_extendedattr(inode,
  1301. sizeof(struct deviceSpec) +
  1302. sizeof(struct regid), 12, 0x3);
  1303. dsea->attrType = cpu_to_le32(12);
  1304. dsea->attrSubtype = 1;
  1305. dsea->attrLength = cpu_to_le32(
  1306. sizeof(struct deviceSpec) +
  1307. sizeof(struct regid));
  1308. dsea->impUseLength = cpu_to_le32(sizeof(struct regid));
  1309. }
  1310. eid = (struct regid *)dsea->impUse;
  1311. memset(eid, 0, sizeof(struct regid));
  1312. strcpy(eid->ident, UDF_ID_DEVELOPER);
  1313. eid->identSuffix[0] = UDF_OS_CLASS_UNIX;
  1314. eid->identSuffix[1] = UDF_OS_ID_LINUX;
  1315. dsea->majorDeviceIdent = cpu_to_le32(imajor(inode));
  1316. dsea->minorDeviceIdent = cpu_to_le32(iminor(inode));
  1317. }
  1318. if (iinfo->i_efe == 0) {
  1319. memcpy(bh->b_data + sizeof(struct fileEntry),
  1320. iinfo->i_ext.i_data,
  1321. inode->i_sb->s_blocksize - sizeof(struct fileEntry));
  1322. fe->logicalBlocksRecorded = cpu_to_le64(
  1323. (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
  1324. (blocksize_bits - 9));
  1325. udf_time_to_disk_stamp(&fe->accessTime, inode->i_atime);
  1326. udf_time_to_disk_stamp(&fe->modificationTime, inode->i_mtime);
  1327. udf_time_to_disk_stamp(&fe->attrTime, inode->i_ctime);
  1328. memset(&(fe->impIdent), 0, sizeof(struct regid));
  1329. strcpy(fe->impIdent.ident, UDF_ID_DEVELOPER);
  1330. fe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
  1331. fe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
  1332. fe->uniqueID = cpu_to_le64(iinfo->i_unique);
  1333. fe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
  1334. fe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
  1335. fe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_FE);
  1336. crclen = sizeof(struct fileEntry);
  1337. } else {
  1338. memcpy(bh->b_data + sizeof(struct extendedFileEntry),
  1339. iinfo->i_ext.i_data,
  1340. inode->i_sb->s_blocksize -
  1341. sizeof(struct extendedFileEntry));
  1342. efe->objectSize = cpu_to_le64(inode->i_size);
  1343. efe->logicalBlocksRecorded = cpu_to_le64(
  1344. (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
  1345. (blocksize_bits - 9));
  1346. if (iinfo->i_crtime.tv_sec > inode->i_atime.tv_sec ||
  1347. (iinfo->i_crtime.tv_sec == inode->i_atime.tv_sec &&
  1348. iinfo->i_crtime.tv_nsec > inode->i_atime.tv_nsec))
  1349. iinfo->i_crtime = inode->i_atime;
  1350. if (iinfo->i_crtime.tv_sec > inode->i_mtime.tv_sec ||
  1351. (iinfo->i_crtime.tv_sec == inode->i_mtime.tv_sec &&
  1352. iinfo->i_crtime.tv_nsec > inode->i_mtime.tv_nsec))
  1353. iinfo->i_crtime = inode->i_mtime;
  1354. if (iinfo->i_crtime.tv_sec > inode->i_ctime.tv_sec ||
  1355. (iinfo->i_crtime.tv_sec == inode->i_ctime.tv_sec &&
  1356. iinfo->i_crtime.tv_nsec > inode->i_ctime.tv_nsec))
  1357. iinfo->i_crtime = inode->i_ctime;
  1358. udf_time_to_disk_stamp(&efe->accessTime, inode->i_atime);
  1359. udf_time_to_disk_stamp(&efe->modificationTime, inode->i_mtime);
  1360. udf_time_to_disk_stamp(&efe->createTime, iinfo->i_crtime);
  1361. udf_time_to_disk_stamp(&efe->attrTime, inode->i_ctime);
  1362. memset(&(efe->impIdent), 0, sizeof(struct regid));
  1363. strcpy(efe->impIdent.ident, UDF_ID_DEVELOPER);
  1364. efe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
  1365. efe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
  1366. efe->uniqueID = cpu_to_le64(iinfo->i_unique);
  1367. efe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
  1368. efe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
  1369. efe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EFE);
  1370. crclen = sizeof(struct extendedFileEntry);
  1371. }
  1372. if (iinfo->i_strat4096) {
  1373. fe->icbTag.strategyType = cpu_to_le16(4096);
  1374. fe->icbTag.strategyParameter = cpu_to_le16(1);
  1375. fe->icbTag.numEntries = cpu_to_le16(2);
  1376. } else {
  1377. fe->icbTag.strategyType = cpu_to_le16(4);
  1378. fe->icbTag.numEntries = cpu_to_le16(1);
  1379. }
  1380. if (S_ISDIR(inode->i_mode))
  1381. fe->icbTag.fileType = ICBTAG_FILE_TYPE_DIRECTORY;
  1382. else if (S_ISREG(inode->i_mode))
  1383. fe->icbTag.fileType = ICBTAG_FILE_TYPE_REGULAR;
  1384. else if (S_ISLNK(inode->i_mode))
  1385. fe->icbTag.fileType = ICBTAG_FILE_TYPE_SYMLINK;
  1386. else if (S_ISBLK(inode->i_mode))
  1387. fe->icbTag.fileType = ICBTAG_FILE_TYPE_BLOCK;
  1388. else if (S_ISCHR(inode->i_mode))
  1389. fe->icbTag.fileType = ICBTAG_FILE_TYPE_CHAR;
  1390. else if (S_ISFIFO(inode->i_mode))
  1391. fe->icbTag.fileType = ICBTAG_FILE_TYPE_FIFO;
  1392. else if (S_ISSOCK(inode->i_mode))
  1393. fe->icbTag.fileType = ICBTAG_FILE_TYPE_SOCKET;
  1394. icbflags = iinfo->i_alloc_type |
  1395. ((inode->i_mode & S_ISUID) ? ICBTAG_FLAG_SETUID : 0) |
  1396. ((inode->i_mode & S_ISGID) ? ICBTAG_FLAG_SETGID : 0) |
  1397. ((inode->i_mode & S_ISVTX) ? ICBTAG_FLAG_STICKY : 0) |
  1398. (le16_to_cpu(fe->icbTag.flags) &
  1399. ~(ICBTAG_FLAG_AD_MASK | ICBTAG_FLAG_SETUID |
  1400. ICBTAG_FLAG_SETGID | ICBTAG_FLAG_STICKY));
  1401. fe->icbTag.flags = cpu_to_le16(icbflags);
  1402. if (sbi->s_udfrev >= 0x0200)
  1403. fe->descTag.descVersion = cpu_to_le16(3);
  1404. else
  1405. fe->descTag.descVersion = cpu_to_le16(2);
  1406. fe->descTag.tagSerialNum = cpu_to_le16(sbi->s_serial_number);
  1407. fe->descTag.tagLocation = cpu_to_le32(
  1408. iinfo->i_location.logicalBlockNum);
  1409. crclen += iinfo->i_lenEAttr + iinfo->i_lenAlloc - sizeof(struct tag);
  1410. fe->descTag.descCRCLength = cpu_to_le16(crclen);
  1411. fe->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)fe + sizeof(struct tag),
  1412. crclen));
  1413. fe->descTag.tagChecksum = udf_tag_checksum(&fe->descTag);
  1414. out:
  1415. set_buffer_uptodate(bh);
  1416. unlock_buffer(bh);
  1417. /* write the data blocks */
  1418. mark_buffer_dirty(bh);
  1419. if (do_sync) {
  1420. sync_dirty_buffer(bh);
  1421. if (buffer_write_io_error(bh)) {
  1422. printk(KERN_WARNING "IO error syncing udf inode "
  1423. "[%s:%08lx]\n", inode->i_sb->s_id,
  1424. inode->i_ino);
  1425. err = -EIO;
  1426. }
  1427. }
  1428. brelse(bh);
  1429. return err;
  1430. }
  1431. struct inode *udf_iget(struct super_block *sb, struct kernel_lb_addr *ino)
  1432. {
  1433. unsigned long block = udf_get_lb_pblock(sb, ino, 0);
  1434. struct inode *inode = iget_locked(sb, block);
  1435. if (!inode)
  1436. return NULL;
  1437. if (inode->i_state & I_NEW) {
  1438. memcpy(&UDF_I(inode)->i_location, ino, sizeof(struct kernel_lb_addr));
  1439. __udf_read_inode(inode);
  1440. unlock_new_inode(inode);
  1441. }
  1442. if (is_bad_inode(inode))
  1443. goto out_iput;
  1444. if (ino->logicalBlockNum >= UDF_SB(sb)->
  1445. s_partmaps[ino->partitionReferenceNum].s_partition_len) {
  1446. udf_debug("block=%d, partition=%d out of range\n",
  1447. ino->logicalBlockNum, ino->partitionReferenceNum);
  1448. make_bad_inode(inode);
  1449. goto out_iput;
  1450. }
  1451. return inode;
  1452. out_iput:
  1453. iput(inode);
  1454. return NULL;
  1455. }
  1456. int8_t udf_add_aext(struct inode *inode, struct extent_position *epos,
  1457. struct kernel_lb_addr *eloc, uint32_t elen, int inc)
  1458. {
  1459. int adsize;
  1460. struct short_ad *sad = NULL;
  1461. struct long_ad *lad = NULL;
  1462. struct allocExtDesc *aed;
  1463. int8_t etype;
  1464. uint8_t *ptr;
  1465. struct udf_inode_info *iinfo = UDF_I(inode);
  1466. if (!epos->bh)
  1467. ptr = iinfo->i_ext.i_data + epos->offset -
  1468. udf_file_entry_alloc_offset(inode) +
  1469. iinfo->i_lenEAttr;
  1470. else
  1471. ptr = epos->bh->b_data + epos->offset;
  1472. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
  1473. adsize = sizeof(struct short_ad);
  1474. else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
  1475. adsize = sizeof(struct long_ad);
  1476. else
  1477. return -1;
  1478. if (epos->offset + (2 * adsize) > inode->i_sb->s_blocksize) {
  1479. unsigned char *sptr, *dptr;
  1480. struct buffer_head *nbh;
  1481. int err, loffset;
  1482. struct kernel_lb_addr obloc = epos->block;
  1483. epos->block.logicalBlockNum = udf_new_block(inode->i_sb, NULL,
  1484. obloc.partitionReferenceNum,
  1485. obloc.logicalBlockNum, &err);
  1486. if (!epos->block.logicalBlockNum)
  1487. return -1;
  1488. nbh = udf_tgetblk(inode->i_sb, udf_get_lb_pblock(inode->i_sb,
  1489. &epos->block,
  1490. 0));
  1491. if (!nbh)
  1492. return -1;
  1493. lock_buffer(nbh);
  1494. memset(nbh->b_data, 0x00, inode->i_sb->s_blocksize);
  1495. set_buffer_uptodate(nbh);
  1496. unlock_buffer(nbh);
  1497. mark_buffer_dirty_inode(nbh, inode);
  1498. aed = (struct allocExtDesc *)(nbh->b_data);
  1499. if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT))
  1500. aed->previousAllocExtLocation =
  1501. cpu_to_le32(obloc.logicalBlockNum);
  1502. if (epos->offset + adsize > inode->i_sb->s_blocksize) {
  1503. loffset = epos->offset;
  1504. aed->lengthAllocDescs = cpu_to_le32(adsize);
  1505. sptr = ptr - adsize;
  1506. dptr = nbh->b_data + sizeof(struct allocExtDesc);
  1507. memcpy(dptr, sptr, adsize);
  1508. epos->offset = sizeof(struct allocExtDesc) + adsize;
  1509. } else {
  1510. loffset = epos->offset + adsize;
  1511. aed->lengthAllocDescs = cpu_to_le32(0);
  1512. sptr = ptr;
  1513. epos->offset = sizeof(struct allocExtDesc);
  1514. if (epos->bh) {
  1515. aed = (struct allocExtDesc *)epos->bh->b_data;
  1516. le32_add_cpu(&aed->lengthAllocDescs, adsize);
  1517. } else {
  1518. iinfo->i_lenAlloc += adsize;
  1519. mark_inode_dirty(inode);
  1520. }
  1521. }
  1522. if (UDF_SB(inode->i_sb)->s_udfrev >= 0x0200)
  1523. udf_new_tag(nbh->b_data, TAG_IDENT_AED, 3, 1,
  1524. epos->block.logicalBlockNum, sizeof(struct tag));
  1525. else
  1526. udf_new_tag(nbh->b_data, TAG_IDENT_AED, 2, 1,
  1527. epos->block.logicalBlockNum, sizeof(struct tag));
  1528. switch (iinfo->i_alloc_type) {
  1529. case ICBTAG_FLAG_AD_SHORT:
  1530. sad = (struct short_ad *)sptr;
  1531. sad->extLength = cpu_to_le32(EXT_NEXT_EXTENT_ALLOCDECS |
  1532. inode->i_sb->s_blocksize);
  1533. sad->extPosition =
  1534. cpu_to_le32(epos->block.logicalBlockNum);
  1535. break;
  1536. case ICBTAG_FLAG_AD_LONG:
  1537. lad = (struct long_ad *)sptr;
  1538. lad->extLength = cpu_to_le32(EXT_NEXT_EXTENT_ALLOCDECS |
  1539. inode->i_sb->s_blocksize);
  1540. lad->extLocation = cpu_to_lelb(epos->block);
  1541. memset(lad->impUse, 0x00, sizeof(lad->impUse));
  1542. break;
  1543. }
  1544. if (epos->bh) {
  1545. if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
  1546. UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
  1547. udf_update_tag(epos->bh->b_data, loffset);
  1548. else
  1549. udf_update_tag(epos->bh->b_data,
  1550. sizeof(struct allocExtDesc));
  1551. mark_buffer_dirty_inode(epos->bh, inode);
  1552. brelse(epos->bh);
  1553. } else {
  1554. mark_inode_dirty(inode);
  1555. }
  1556. epos->bh = nbh;
  1557. }
  1558. etype = udf_write_aext(inode, epos, eloc, elen, inc);
  1559. if (!epos->bh) {
  1560. iinfo->i_lenAlloc += adsize;
  1561. mark_inode_dirty(inode);
  1562. } else {
  1563. aed = (struct allocExtDesc *)epos->bh->b_data;
  1564. le32_add_cpu(&aed->lengthAllocDescs, adsize);
  1565. if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
  1566. UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
  1567. udf_update_tag(epos->bh->b_data,
  1568. epos->offset + (inc ? 0 : adsize));
  1569. else
  1570. udf_update_tag(epos->bh->b_data,
  1571. sizeof(struct allocExtDesc));
  1572. mark_buffer_dirty_inode(epos->bh, inode);
  1573. }
  1574. return etype;
  1575. }
  1576. int8_t udf_write_aext(struct inode *inode, struct extent_position *epos,
  1577. struct kernel_lb_addr *eloc, uint32_t elen, int inc)
  1578. {
  1579. int adsize;
  1580. uint8_t *ptr;
  1581. struct short_ad *sad;
  1582. struct long_ad *lad;
  1583. struct udf_inode_info *iinfo = UDF_I(inode);
  1584. if (!epos->bh)
  1585. ptr = iinfo->i_ext.i_data + epos->offset -
  1586. udf_file_entry_alloc_offset(inode) +
  1587. iinfo->i_lenEAttr;
  1588. else
  1589. ptr = epos->bh->b_data + epos->offset;
  1590. switch (iinfo->i_alloc_type) {
  1591. case ICBTAG_FLAG_AD_SHORT:
  1592. sad = (struct short_ad *)ptr;
  1593. sad->extLength = cpu_to_le32(elen);
  1594. sad->extPosition = cpu_to_le32(eloc->logicalBlockNum);
  1595. adsize = sizeof(struct short_ad);
  1596. break;
  1597. case ICBTAG_FLAG_AD_LONG:
  1598. lad = (struct long_ad *)ptr;
  1599. lad->extLength = cpu_to_le32(elen);
  1600. lad->extLocation = cpu_to_lelb(*eloc);
  1601. memset(lad->impUse, 0x00, sizeof(lad->impUse));
  1602. adsize = sizeof(struct long_ad);
  1603. break;
  1604. default:
  1605. return -1;
  1606. }
  1607. if (epos->bh) {
  1608. if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
  1609. UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) {
  1610. struct allocExtDesc *aed =
  1611. (struct allocExtDesc *)epos->bh->b_data;
  1612. udf_update_tag(epos->bh->b_data,
  1613. le32_to_cpu(aed->lengthAllocDescs) +
  1614. sizeof(struct allocExtDesc));
  1615. }
  1616. mark_buffer_dirty_inode(epos->bh, inode);
  1617. } else {
  1618. mark_inode_dirty(inode);
  1619. }
  1620. if (inc)
  1621. epos->offset += adsize;
  1622. return (elen >> 30);
  1623. }
  1624. int8_t udf_next_aext(struct inode *inode, struct extent_position *epos,
  1625. struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
  1626. {
  1627. int8_t etype;
  1628. while ((etype = udf_current_aext(inode, epos, eloc, elen, inc)) ==
  1629. (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) {
  1630. int block;
  1631. epos->block = *eloc;
  1632. epos->offset = sizeof(struct allocExtDesc);
  1633. brelse(epos->bh);
  1634. block = udf_get_lb_pblock(inode->i_sb, &epos->block, 0);
  1635. epos->bh = udf_tread(inode->i_sb, block);
  1636. if (!epos->bh) {
  1637. udf_debug("reading block %d failed!\n", block);
  1638. return -1;
  1639. }
  1640. }
  1641. return etype;
  1642. }
  1643. int8_t udf_current_aext(struct inode *inode, struct extent_position *epos,
  1644. struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
  1645. {
  1646. int alen;
  1647. int8_t etype;
  1648. uint8_t *ptr;
  1649. struct short_ad *sad;
  1650. struct long_ad *lad;
  1651. struct udf_inode_info *iinfo = UDF_I(inode);
  1652. if (!epos->bh) {
  1653. if (!epos->offset)
  1654. epos->offset = udf_file_entry_alloc_offset(inode);
  1655. ptr = iinfo->i_ext.i_data + epos->offset -
  1656. udf_file_entry_alloc_offset(inode) +
  1657. iinfo->i_lenEAttr;
  1658. alen = udf_file_entry_alloc_offset(inode) +
  1659. iinfo->i_lenAlloc;
  1660. } else {
  1661. if (!epos->offset)
  1662. epos->offset = sizeof(struct allocExtDesc);
  1663. ptr = epos->bh->b_data + epos->offset;
  1664. alen = sizeof(struct allocExtDesc) +
  1665. le32_to_cpu(((struct allocExtDesc *)epos->bh->b_data)->
  1666. lengthAllocDescs);
  1667. }
  1668. switch (iinfo->i_alloc_type) {
  1669. case ICBTAG_FLAG_AD_SHORT:
  1670. sad = udf_get_fileshortad(ptr, alen, &epos->offset, inc);
  1671. if (!sad)
  1672. return -1;
  1673. etype = le32_to_cpu(sad->extLength) >> 30;
  1674. eloc->logicalBlockNum = le32_to_cpu(sad->extPosition);
  1675. eloc->partitionReferenceNum =
  1676. iinfo->i_location.partitionReferenceNum;
  1677. *elen = le32_to_cpu(sad->extLength) & UDF_EXTENT_LENGTH_MASK;
  1678. break;
  1679. case ICBTAG_FLAG_AD_LONG:
  1680. lad = udf_get_filelongad(ptr, alen, &epos->offset, inc);
  1681. if (!lad)
  1682. return -1;
  1683. etype = le32_to_cpu(lad->extLength) >> 30;
  1684. *eloc = lelb_to_cpu(lad->extLocation);
  1685. *elen = le32_to_cpu(lad->extLength) & UDF_EXTENT_LENGTH_MASK;
  1686. break;
  1687. default:
  1688. udf_debug("alloc_type = %d unsupported\n",
  1689. iinfo->i_alloc_type);
  1690. return -1;
  1691. }
  1692. return etype;
  1693. }
  1694. static int8_t udf_insert_aext(struct inode *inode, struct extent_position epos,
  1695. struct kernel_lb_addr neloc, uint32_t nelen)
  1696. {
  1697. struct kernel_lb_addr oeloc;
  1698. uint32_t oelen;
  1699. int8_t etype;
  1700. if (epos.bh)
  1701. get_bh(epos.bh);
  1702. while ((etype = udf_next_aext(inode, &epos, &oeloc, &oelen, 0)) != -1) {
  1703. udf_write_aext(inode, &epos, &neloc, nelen, 1);
  1704. neloc = oeloc;
  1705. nelen = (etype << 30) | oelen;
  1706. }
  1707. udf_add_aext(inode, &epos, &neloc, nelen, 1);
  1708. brelse(epos.bh);
  1709. return (nelen >> 30);
  1710. }
  1711. int8_t udf_delete_aext(struct inode *inode, struct extent_position epos,
  1712. struct kernel_lb_addr eloc, uint32_t elen)
  1713. {
  1714. struct extent_position oepos;
  1715. int adsize;
  1716. int8_t etype;
  1717. struct allocExtDesc *aed;
  1718. struct udf_inode_info *iinfo;
  1719. if (epos.bh) {
  1720. get_bh(epos.bh);
  1721. get_bh(epos.bh);
  1722. }
  1723. iinfo = UDF_I(inode);
  1724. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
  1725. adsize = sizeof(struct short_ad);
  1726. else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
  1727. adsize = sizeof(struct long_ad);
  1728. else
  1729. adsize = 0;
  1730. oepos = epos;
  1731. if (udf_next_aext(inode, &epos, &eloc, &elen, 1) == -1)
  1732. return -1;
  1733. while ((etype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
  1734. udf_write_aext(inode, &oepos, &eloc, (etype << 30) | elen, 1);
  1735. if (oepos.bh != epos.bh) {
  1736. oepos.block = epos.block;
  1737. brelse(oepos.bh);
  1738. get_bh(epos.bh);
  1739. oepos.bh = epos.bh;
  1740. oepos.offset = epos.offset - adsize;
  1741. }
  1742. }
  1743. memset(&eloc, 0x00, sizeof(struct kernel_lb_addr));
  1744. elen = 0;
  1745. if (epos.bh != oepos.bh) {
  1746. udf_free_blocks(inode->i_sb, inode, &epos.block, 0, 1);
  1747. udf_write_aext(inode, &oepos, &eloc, elen, 1);
  1748. udf_write_aext(inode, &oepos, &eloc, elen, 1);
  1749. if (!oepos.bh) {
  1750. iinfo->i_lenAlloc -= (adsize * 2);
  1751. mark_inode_dirty(inode);
  1752. } else {
  1753. aed = (struct allocExtDesc *)oepos.bh->b_data;
  1754. le32_add_cpu(&aed->lengthAllocDescs, -(2 * adsize));
  1755. if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
  1756. UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
  1757. udf_update_tag(oepos.bh->b_data,
  1758. oepos.offset - (2 * adsize));
  1759. else
  1760. udf_update_tag(oepos.bh->b_data,
  1761. sizeof(struct allocExtDesc));
  1762. mark_buffer_dirty_inode(oepos.bh, inode);
  1763. }
  1764. } else {
  1765. udf_write_aext(inode, &oepos, &eloc, elen, 1);
  1766. if (!oepos.bh) {
  1767. iinfo->i_lenAlloc -= adsize;
  1768. mark_inode_dirty(inode);
  1769. } else {
  1770. aed = (struct allocExtDesc *)oepos.bh->b_data;
  1771. le32_add_cpu(&aed->lengthAllocDescs, -adsize);
  1772. if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
  1773. UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
  1774. udf_update_tag(oepos.bh->b_data,
  1775. epos.offset - adsize);
  1776. else
  1777. udf_update_tag(oepos.bh->b_data,
  1778. sizeof(struct allocExtDesc));
  1779. mark_buffer_dirty_inode(oepos.bh, inode);
  1780. }
  1781. }
  1782. brelse(epos.bh);
  1783. brelse(oepos.bh);
  1784. return (elen >> 30);
  1785. }
  1786. int8_t inode_bmap(struct inode *inode, sector_t block,
  1787. struct extent_position *pos, struct kernel_lb_addr *eloc,
  1788. uint32_t *elen, sector_t *offset)
  1789. {
  1790. unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
  1791. loff_t lbcount = 0, bcount =
  1792. (loff_t) block << blocksize_bits;
  1793. int8_t etype;
  1794. struct udf_inode_info *iinfo;
  1795. iinfo = UDF_I(inode);
  1796. pos->offset = 0;
  1797. pos->block = iinfo->i_location;
  1798. pos->bh = NULL;
  1799. *elen = 0;
  1800. do {
  1801. etype = udf_next_aext(inode, pos, eloc, elen, 1);
  1802. if (etype == -1) {
  1803. *offset = (bcount - lbcount) >> blocksize_bits;
  1804. iinfo->i_lenExtents = lbcount;
  1805. return -1;
  1806. }
  1807. lbcount += *elen;
  1808. } while (lbcount <= bcount);
  1809. *offset = (bcount + *elen - lbcount) >> blocksize_bits;
  1810. return etype;
  1811. }
  1812. long udf_block_map(struct inode *inode, sector_t block)
  1813. {
  1814. struct kernel_lb_addr eloc;
  1815. uint32_t elen;
  1816. sector_t offset;
  1817. struct extent_position epos = {};
  1818. int ret;
  1819. lock_kernel();
  1820. if (inode_bmap(inode, block, &epos, &eloc, &elen, &offset) ==
  1821. (EXT_RECORDED_ALLOCATED >> 30))
  1822. ret = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
  1823. else
  1824. ret = 0;
  1825. unlock_kernel();
  1826. brelse(epos.bh);
  1827. if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_VARCONV))
  1828. return udf_fixed_to_variable(ret);
  1829. else
  1830. return ret;
  1831. }