inode.c 58 KB

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