inode.c 54 KB

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