inode.c 57 KB

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