inode.c 59 KB

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