inode.c 57 KB

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