inode.c 59 KB

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