inode.c 57 KB

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