inode.c 58 KB

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