inode.c 59 KB

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