inode.c 57 KB

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