inode.c 63 KB

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