inode.c 59 KB

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