inode.c 65 KB

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